| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
package joptsimple; |
| 7 | |
|
| 8 | |
import java.util.Iterator; |
| 9 | |
import java.util.List; |
| 10 | |
import java.util.Map; |
| 11 | |
import java.util.TreeMap; |
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
|
| 20 | |
|
| 21 | |
|
| 22 | |
|
| 23 | |
|
| 24 | |
|
| 25 | |
|
| 26 | |
|
| 27 | |
|
| 28 | |
|
| 29 | |
|
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | 2870 | class AbbreviationMap { |
| 47 | |
private String key; |
| 48 | |
private Object value; |
| 49 | 2870 | private final Map children = new TreeMap(); |
| 50 | |
private int keysBeyond; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
boolean contains( String aKey ) { |
| 61 | 636 | return get( aKey ) != null; |
| 62 | |
} |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
Object get( String aKey ) { |
| 75 | 1250 | char[] chars = charsOf( aKey ); |
| 76 | |
|
| 77 | 1248 | AbbreviationMap child = this; |
| 78 | 4100 | for ( int i = 0; i < chars.length; ++i ) { |
| 79 | 2940 | child = (AbbreviationMap) child.children.get( new Character( chars[ i ] ) ); |
| 80 | 2940 | if ( child == null ) |
| 81 | 88 | return null; |
| 82 | |
} |
| 83 | |
|
| 84 | 1160 | return child.value; |
| 85 | |
} |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
void put( String aKey, Object newValue ) { |
| 98 | 1198 | if ( newValue == null ) |
| 99 | 2 | throw new NullPointerException(); |
| 100 | 1196 | if ( aKey.length() == 0 ) |
| 101 | 2 | throw new IllegalArgumentException(); |
| 102 | |
|
| 103 | 1192 | char[] chars = charsOf( aKey ); |
| 104 | 1192 | add( chars, newValue, 0, chars.length ); |
| 105 | 1192 | } |
| 106 | |
|
| 107 | |
void putAll( List keys, Object newValue ) { |
| 108 | 886 | for ( Iterator iter = keys.iterator(); iter.hasNext(); ) |
| 109 | 1062 | put( (String) iter.next(), newValue ); |
| 110 | 886 | } |
| 111 | |
|
| 112 | |
private boolean add( char[] chars, Object newValue, int offset, int length ) { |
| 113 | 5136 | if ( offset == length ) { |
| 114 | 1192 | value = newValue; |
| 115 | 1192 | boolean wasAlreadyAKey = key != null; |
| 116 | 1192 | key = new String( chars ); |
| 117 | 1192 | return !wasAlreadyAKey; |
| 118 | |
} |
| 119 | |
|
| 120 | 3944 | Character nextChar = new Character( chars[ offset ] ); |
| 121 | 3944 | AbbreviationMap child = (AbbreviationMap) children.get( nextChar ); |
| 122 | 3944 | if ( child == null ) { |
| 123 | 2466 | child = new AbbreviationMap(); |
| 124 | 2466 | children.put( nextChar, child ); |
| 125 | |
} |
| 126 | |
|
| 127 | 3944 | boolean newKeyAdded = child.add( chars, newValue, offset + 1, length ); |
| 128 | |
|
| 129 | 3944 | if ( newKeyAdded ) |
| 130 | 2782 | ++keysBeyond; |
| 131 | |
|
| 132 | 3944 | if ( key == null ) |
| 133 | 3814 | value = keysBeyond > 1 ? null : newValue; |
| 134 | |
|
| 135 | 3944 | return newKeyAdded; |
| 136 | |
} |
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
|
| 143 | |
|
| 144 | |
|
| 145 | |
void remove( String aKey ) { |
| 146 | 24 | if ( aKey.length() == 0 ) |
| 147 | 2 | throw new IllegalArgumentException(); |
| 148 | |
|
| 149 | 20 | char[] keyChars = charsOf( aKey ); |
| 150 | 20 | remove( keyChars, 0, keyChars.length ); |
| 151 | 20 | } |
| 152 | |
|
| 153 | |
private boolean remove( char[] aKey, int offset, int length ) { |
| 154 | 78 | if ( offset == length ) |
| 155 | 16 | return removeAtEndOfKey(); |
| 156 | |
|
| 157 | 62 | Character nextChar = new Character( aKey[ offset ] ); |
| 158 | 62 | AbbreviationMap child = (AbbreviationMap) children.get( nextChar ); |
| 159 | 62 | if ( child == null || !child.remove( aKey, offset + 1, length ) ) |
| 160 | 14 | return false; |
| 161 | |
|
| 162 | 48 | --keysBeyond; |
| 163 | 48 | if ( child.keysBeyond == 0 ) |
| 164 | 12 | children.remove( nextChar ); |
| 165 | 48 | if ( keysBeyond == 1 && key == null ) |
| 166 | 14 | setValueToThatOfOnlyChild(); |
| 167 | |
|
| 168 | 48 | return true; |
| 169 | |
} |
| 170 | |
|
| 171 | |
private void setValueToThatOfOnlyChild() { |
| 172 | 18 | Map.Entry entry = (Map.Entry) children.entrySet().iterator().next(); |
| 173 | 18 | AbbreviationMap onlyChild = (AbbreviationMap) entry.getValue(); |
| 174 | 18 | value = onlyChild.value; |
| 175 | 18 | } |
| 176 | |
|
| 177 | |
private boolean removeAtEndOfKey() { |
| 178 | 16 | if ( key == null ) |
| 179 | 4 | return false; |
| 180 | |
|
| 181 | 12 | key = null; |
| 182 | 12 | if ( keysBeyond == 1 ) |
| 183 | 4 | setValueToThatOfOnlyChild(); |
| 184 | |
else |
| 185 | 8 | value = null; |
| 186 | |
|
| 187 | 12 | return true; |
| 188 | |
} |
| 189 | |
|
| 190 | |
Map toJavaUtilMap() { |
| 191 | 66 | Map mappings = new TreeMap(); |
| 192 | 66 | addToMappings( mappings ); |
| 193 | 66 | return mappings; |
| 194 | |
} |
| 195 | |
|
| 196 | |
private void addToMappings( Map mappings ) { |
| 197 | 792 | if ( key != null ) |
| 198 | 194 | mappings.put( key, value ); |
| 199 | |
|
| 200 | 792 | for ( Iterator iter = children.values().iterator(); iter.hasNext(); ) |
| 201 | 726 | ( (AbbreviationMap) iter.next() ).addToMappings( mappings ); |
| 202 | 792 | } |
| 203 | |
|
| 204 | |
private static char[] charsOf( String aKey ) { |
| 205 | 2462 | char[] chars = new char[ aKey.length() ]; |
| 206 | 2460 | aKey.getChars( 0, aKey.length(), chars, 0 ); |
| 207 | 2460 | return chars; |
| 208 | |
} |
| 209 | |
} |