1 19 20 package org.netbeans.api.lexer; 21 22 import java.util.HashMap ; 23 import java.util.Map ; 24 25 54 55 public final class InputAttributes { 56 57 private final Map <LanguagePath,LPAttrs> lp2attrs; 58 59 public InputAttributes() { 60 lp2attrs = new HashMap <LanguagePath,LPAttrs>(); 61 } 62 63 77 public Object getValue(LanguagePath languagePath, Object attributeKey) { 78 checkAttributeKeyNotNull(attributeKey); 79 synchronized (lp2attrs) { 80 LPAttrs attrs = lp2attrs.get(languagePath); 81 Object value = null; 82 if (attrs != null) { 83 value = attrs.getSpecific(attributeKey); 84 if (value == null) { value = attrs.getGlobal(attributeKey); 86 } 87 } 88 while (value == null && languagePath.size() > 1) { 90 languagePath = languagePath.subPath(1); 91 attrs = lp2attrs.get(languagePath); 92 if (attrs != null) { 93 value = attrs.getGlobal(attributeKey); 94 } 95 } 96 return value; 97 } 98 } 99 100 106 public void setValue(Language<? extends TokenId> language, 107 Object attributeKey, Object attributeValue, boolean global) { 108 setValue(LanguagePath.get(language), attributeKey, attributeValue, global); 109 } 110 111 129 public void setValue(LanguagePath languagePath, 130 Object attributeKey, Object attributeValue, boolean global) { 131 checkAttributeKeyNotNull(attributeKey); 132 synchronized (lp2attrs) { 133 LPAttrs attrs = lp2attrs.get(languagePath); 134 if (attrs == null) { 135 attrs = new LPAttrs(); 136 lp2attrs.put(languagePath, attrs); 137 } 138 if (global) { 139 attrs.putGlobal(attributeKey, attributeValue); 140 } else { 141 attrs.putSpecific(attributeKey, attributeValue); 142 } 143 } 144 } 145 146 private void checkAttributeKeyNotNull(Object attributeKey) { 147 if (attributeKey == null) { 148 throw new IllegalArgumentException ("attributeKey cannot be null"); 149 } 150 } 151 152 private static final class LPAttrs { 153 154 private Map <Object ,Object > specifics; 155 156 private Map <Object ,Object > globals; 157 158 public Object getSpecific(Object key) { 159 return (specifics != null) ? specifics.get(key) : null; 160 } 161 162 public Object getGlobal(Object key) { 163 return (globals != null) ? globals.get(key) : null; 164 } 165 166 public void putSpecific(Object key, Object value) { 167 if (specifics == null) { 168 specifics = new HashMap <Object ,Object >(4); 169 } 170 specifics.put(key, value); 171 } 172 173 public void putGlobal(Object key, Object value) { 174 if (globals == null) { 175 globals = new HashMap <Object ,Object >(4); 176 } 177 globals.put(key, value); 178 } 179 180 } 181 182 } 183 | Popular Tags |