001 package org.crsh.cmdline.spi; 002 003 import java.io.Serializable; 004 import java.util.Iterator; 005 import java.util.LinkedHashMap; 006 import java.util.Map; 007 import java.util.Set; 008 009 /** 010 * A completion result. 011 * 012 * @author <a href="mailto:julien.viet@exoplatform.com">Julien Viet</a> 013 */ 014 public final class ValueCompletion implements Iterable<Map.Entry<String, Boolean>>, Serializable { 015 016 public static ValueCompletion create() { 017 return new ValueCompletion(); 018 } 019 020 public static ValueCompletion create(String prefix) { 021 return new ValueCompletion(prefix); 022 } 023 024 public static ValueCompletion create(String prefix, String suffix, boolean value) { 025 ValueCompletion result = new ValueCompletion(prefix); 026 result.put(suffix, value); 027 return result; 028 } 029 030 public static ValueCompletion create(String suffix, boolean value) { 031 ValueCompletion result = new ValueCompletion(); 032 result.put(suffix, value); 033 return result; 034 } 035 036 /** . */ 037 private final String prefix; 038 039 /** . */ 040 private final Map<String, Boolean> entries; 041 042 public ValueCompletion() { 043 this(""); 044 } 045 046 public ValueCompletion(String prefix) { 047 this(prefix, new LinkedHashMap<String, Boolean>()); 048 } 049 050 public ValueCompletion(String prefix, Map<String, Boolean> entries) { 051 if (prefix == null) { 052 throw new NullPointerException("No null prefix allowed"); 053 } 054 if (entries == null) { 055 throw new NullPointerException("No null values allowed"); 056 } 057 058 // 059 this.prefix = prefix; 060 this.entries = entries; 061 } 062 063 public Iterator<Map.Entry<String, Boolean>> iterator() { 064 return entries.entrySet().iterator(); 065 } 066 067 public Set<String> getSuffixes() { 068 return entries.keySet(); 069 } 070 071 public boolean isEmpty() { 072 return entries.isEmpty(); 073 } 074 075 public Object get(String key) { 076 return entries.get(key); 077 } 078 079 public int getSize() { 080 return entries.size(); 081 } 082 083 public ValueCompletion put(String key, boolean value) { 084 entries.put(key, value); 085 return this; 086 } 087 088 public String getPrefix() { 089 return prefix; 090 } 091 092 @Override 093 public int hashCode() { 094 return prefix.hashCode() ^ entries.hashCode(); 095 } 096 097 @Override 098 public boolean equals(Object obj) { 099 if (obj == this) { 100 return true; 101 } 102 if (obj instanceof ValueCompletion) { 103 ValueCompletion that = (ValueCompletion)obj; 104 return prefix.equals(that.prefix) && entries.equals(that.entries); 105 } 106 return false; 107 } 108 109 @Override 110 public String toString() { 111 return "Completion[prefix=" + prefix + ",entries=" + entries + "]"; 112 } 113 }