001    /*
002     * Copyright (C) 2012 eXo Platform SAS.
003     *
004     * This is free software; you can redistribute it and/or modify it
005     * under the terms of the GNU Lesser General Public License as
006     * published by the Free Software Foundation; either version 2.1 of
007     * the License, or (at your option) any later version.
008     *
009     * This software is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012     * Lesser General Public License for more details.
013     *
014     * You should have received a copy of the GNU Lesser General Public
015     * License along with this software; if not, write to the Free
016     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
017     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
018     */
019    
020    package org.crsh.cmdline.spi;
021    
022    import java.io.Serializable;
023    import java.util.Iterator;
024    import java.util.LinkedHashMap;
025    import java.util.Map;
026    import java.util.Set;
027    
028    public final class ValueCompletion implements Iterable<Map.Entry<String, Boolean>>, Serializable {
029    
030      public static ValueCompletion create() {
031        return new ValueCompletion();
032      }
033    
034      public static ValueCompletion create(String prefix) {
035        return new ValueCompletion(prefix);
036      }
037    
038      public static ValueCompletion create(String prefix, String suffix, boolean value) {
039        ValueCompletion result = new ValueCompletion(prefix);
040        result.put(suffix, value);
041        return result;
042      }
043    
044      public static ValueCompletion create(String suffix, boolean value) {
045        ValueCompletion result = new ValueCompletion();
046        result.put(suffix, value);
047        return result;
048      }
049    
050      /** . */
051      private final String prefix;
052    
053      /** . */
054      private final Map<String, Boolean> entries;
055    
056      public ValueCompletion() {
057        this("");
058      }
059    
060      public ValueCompletion(String prefix) {
061        this(prefix, new LinkedHashMap<String, Boolean>());
062      }
063    
064      public ValueCompletion(String prefix, Map<String, Boolean> entries) {
065        if (prefix == null) {
066          throw new NullPointerException("No null prefix allowed");
067        }
068        if (entries == null) {
069          throw new NullPointerException("No null values allowed");
070        }
071    
072        //
073        this.prefix = prefix;
074        this.entries = entries;
075      }
076    
077      public Iterator<Map.Entry<String, Boolean>> iterator() {
078        return entries.entrySet().iterator();
079      }
080    
081      public Set<String> getSuffixes() {
082        return entries.keySet();
083      }
084    
085      public boolean isEmpty() {
086        return entries.isEmpty();
087      }
088    
089      public Object get(String key) {
090        return entries.get(key);
091      }
092    
093      public int getSize() {
094        return entries.size();
095      }
096    
097      public ValueCompletion put(String key, boolean value) {
098        entries.put(key, value);
099        return this;
100      }
101    
102      public String getPrefix() {
103        return prefix;
104      }
105    
106      @Override
107      public int hashCode() {
108        return prefix.hashCode() ^ entries.hashCode();
109      }
110    
111      @Override
112      public boolean equals(Object obj) {
113        if (obj == this) {
114          return true;
115        }
116        if (obj instanceof ValueCompletion) {
117          ValueCompletion that = (ValueCompletion)obj;
118          return prefix.equals(that.prefix) && entries.equals(that.entries);
119        }
120        return false;
121      }
122    
123      @Override
124      public String toString() {
125        return "Completion[prefix=" + prefix + ",entries=" + entries + "]";
126      }
127    }