001package gudusoft.gsqlparser.common.structured; 002 003import java.util.Objects; 004 005public final class StructuredPathSegment { 006 007 private final StructuredPathSegmentKind kind; 008 private final String name; 009 010 private StructuredPathSegment(StructuredPathSegmentKind kind, String name) { 011 this.kind = kind; 012 this.name = name; 013 } 014 015 public static StructuredPathSegment arrayElement() { 016 return new StructuredPathSegment(StructuredPathSegmentKind.ARRAY_ELEMENT, null); 017 } 018 019 public static StructuredPathSegment field(String name) { 020 if (name == null || name.isEmpty()) { 021 throw new IllegalArgumentException("field name must be non-empty"); 022 } 023 return new StructuredPathSegment(StructuredPathSegmentKind.FIELD, name); 024 } 025 026 public static StructuredPathSegment mapKey() { 027 return new StructuredPathSegment(StructuredPathSegmentKind.MAP_KEY, null); 028 } 029 030 public static StructuredPathSegment mapValue() { 031 return new StructuredPathSegment(StructuredPathSegmentKind.MAP_VALUE, null); 032 } 033 034 public StructuredPathSegmentKind getKind() { 035 return kind; 036 } 037 038 public String getName() { 039 return name; 040 } 041 042 public String toDisplayString() { 043 switch (kind) { 044 case ARRAY_ELEMENT: return "[*]"; 045 case FIELD: return "." + name; 046 case MAP_KEY: return "[key]"; 047 case MAP_VALUE: return "[value]"; 048 default: throw new IllegalStateException("unknown kind " + kind); 049 } 050 } 051 052 @Override 053 public boolean equals(Object o) { 054 if (this == o) return true; 055 if (!(o instanceof StructuredPathSegment)) return false; 056 StructuredPathSegment other = (StructuredPathSegment) o; 057 return kind == other.kind && Objects.equals(name, other.name); 058 } 059 060 @Override 061 public int hashCode() { 062 return Objects.hash(kind, name); 063 } 064 065 @Override 066 public String toString() { 067 return toDisplayString(); 068 } 069}