001package gudusoft.gsqlparser.common.structured;
002
003import java.util.ArrayList;
004import java.util.Collections;
005import java.util.List;
006import java.util.Objects;
007
008public final class StructuredStructType implements StructuredType {
009
010    private final List<StructuredStructField> fields;
011
012    public StructuredStructType(List<StructuredStructField> fields) {
013        if (fields == null) {
014            throw new IllegalArgumentException("fields must not be null");
015        }
016        this.fields = Collections.unmodifiableList(new ArrayList<>(fields));
017    }
018
019    public List<StructuredStructField> getFields() {
020        return fields;
021    }
022
023    public StructuredStructField findField(String name) {
024        if (name == null) return null;
025        for (StructuredStructField f : fields) {
026            if (f.getName().equalsIgnoreCase(name)) return f;
027        }
028        return null;
029    }
030
031    @Override
032    public String toDisplayString() {
033        StringBuilder sb = new StringBuilder("STRUCT<");
034        boolean first = true;
035        for (StructuredStructField f : fields) {
036            if (!first) sb.append(", ");
037            sb.append(f.getName()).append(": ").append(f.getType().toDisplayString());
038            first = false;
039        }
040        sb.append('>');
041        return sb.toString();
042    }
043
044    @Override
045    public boolean equals(Object o) {
046        if (this == o) return true;
047        if (!(o instanceof StructuredStructType)) return false;
048        return fields.equals(((StructuredStructType) o).fields);
049    }
050
051    @Override
052    public int hashCode() {
053        return Objects.hash("STRUCT", fields);
054    }
055
056    @Override
057    public String toString() {
058        return toDisplayString();
059    }
060}