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