#nullable enable using System; using System.Collections.Generic; namespace UVC.Data { /// /// URL 주소를 키-값 쌍으로 저장하고 관리하는 클래스입니다. /// public static class URLList { /// /// URL을 저장하는 내부 Dictionary 컬렉션 /// private static Dictionary urls = new Dictionary(); public static IReadOnlyDictionary Urls => urls; /// /// 지정된 키와 URL을 컬렉션에 추가합니다. 키가 이미 존재하면 URL을 업데이트합니다. /// /// URL에 접근하기 위한 고유 키 /// 저장할 URL 문자열 /// /// /// URLList.AddChild("google", "https://www.google.com"); /// URLList.AddChild("naver", "https://www.naver.com"); /// /// // 기존 키의 URL 업데이트 /// URLList.AddChild("google", "https://www.google.co.kr"); /// /// public static void Add(string key, string url) { if (!urls.ContainsKey(key)) { urls.Add(key, url); } else { urls[key] = url; // Update existing URL } } /// /// 지정된 키에 해당하는 URL을 컬렉션에서 제거합니다. /// /// 제거할 URL의 키 /// /// /// URLList.AddChild("temp", "https://www.temp.com"); /// /// // URL 제거 /// URLList.RemoveChild("temp"); /// /// // 존재하지 않는 키 제거 시도(아무 일도 발생하지 않음) /// URLList.RemoveChild("nonexistent"); /// /// public static void Remove(string key) { if (urls.ContainsKey(key)) { urls.Remove(key); } } /// /// 지정된 키가 컬렉션에 존재하는지 확인합니다. /// /// 확인할 키 /// 키가 존재하면 true, 그렇지 않으면 false /// /// /// URLList.AddChild("github", "https://github.com"); /// /// // 키 존재 여부 확인 /// if(URLList.ContainsKey("github")) /// { /// Console.WriteLine("GitHub URL이 존재합니다."); /// } /// /// if(!URLList.ContainsKey("gitlab")) /// { /// Console.WriteLine("GitLab URL이 존재하지 않습니다."); /// } /// /// public static bool ContainsKey(string key) { return urls.ContainsKey(key); } /// /// 지정된 키에 해당하는 URL을 반환합니다. /// /// 가져올 URL의 키 /// 키에 해당하는 URL 또는 키가 존재하지 않을 경우 null /// /// /// URLList.AddChild("youtube", "https://www.youtube.com"); /// /// // URL 가져오기 /// string youtubeUrl = URLList.Get("youtube"); /// if (youtubeUrl != null) /// { /// Console.WriteLine($"YouTube URL: {youtubeUrl}"); /// } /// /// // 존재하지 않는 키로 URL 가져오기 /// string nullUrl = URLList.Get("nonexistent"); /// if (nullUrl == null) /// { /// Console.WriteLine("URL이 존재하지 않습니다."); /// } /// /// public static string? Get(string key) { if (urls.TryGetValue(key, out string url)) { return url; } return null; // or throw an exception if preferred } /// /// URL에서 도메인 부분을 추출합니다. /// /// 도메인을 추출할 URL /// 추출된 도메인 또는 파싱 실패 시 null /// /// /// string domain = URLList.ExtractDomain("https://www.example.com/path/page.html"); /// Console.WriteLine(domain); // 출력: www.example.com /// /// public static string? ExtractDomain(string url) { if (string.IsNullOrEmpty(url)) return null; try { Uri uri = new Uri(url); return uri.Host; } catch (UriFormatException) { return null; } } } }