001package gudusoft.gsqlparser.resolver2.scope;
002
003import gudusoft.gsqlparser.resolver2.namespace.INamespace;
004import gudusoft.gsqlparser.resolver2.model.ResolvePath;
005
006import java.util.ArrayList;
007import java.util.List;
008
009/**
010 * Default implementation of IResolved that collects all matches.
011 */
012public class ResolvedImpl implements IResolved {
013
014    /** All found matches */
015    private final List<Match> matches = new ArrayList<>();
016
017    @Override
018    public void found(INamespace namespace,
019                     boolean nullable,
020                     IScope scope,
021                     ResolvePath path,
022                     List<String> remainingNames) {
023        matches.add(new Match(namespace, nullable, scope, path, remainingNames));
024    }
025
026    @Override
027    public int getCount() {
028        return matches.size();
029    }
030
031    @Override
032    public boolean isUnique() {
033        return matches.size() == 1;
034    }
035
036    @Override
037    public boolean isAmbiguous() {
038        return matches.size() > 1;
039    }
040
041    @Override
042    public boolean isEmpty() {
043        return matches.isEmpty();
044    }
045
046    /**
047     * Get all matches
048     */
049    public List<Match> getMatches() {
050        return new ArrayList<>(matches);
051    }
052
053    /**
054     * Get the first (and possibly only) match
055     */
056    public Match getFirst() {
057        return matches.isEmpty() ? null : matches.get(0);
058    }
059
060    /**
061     * Clear all matches (for reuse)
062     */
063    public void clear() {
064        matches.clear();
065    }
066
067    /**
068     * Represents a single match found during resolution
069     */
070    public static class Match {
071        public final INamespace namespace;
072        public final boolean nullable;
073        public final IScope scope;
074        public final ResolvePath path;
075        public final List<String> remainingNames;
076
077        public Match(INamespace namespace,
078                    boolean nullable,
079                    IScope scope,
080                    ResolvePath path,
081                    List<String> remainingNames) {
082            this.namespace = namespace;
083            this.nullable = nullable;
084            this.scope = scope;
085            this.path = path;
086            this.remainingNames = new ArrayList<>(remainingNames);
087        }
088
089        @Override
090        public String toString() {
091            return String.format("Match{namespace=%s, path=%s}",
092                namespace.getDisplayName(), path);
093        }
094    }
095}