01: /*
02: * Copyright 2006 Google Inc.
03: *
04: * Licensed under the Apache License, Version 2.0 (the "License"); you may not
05: * use this file except in compliance with the License. You may obtain a copy of
06: * the License at
07: *
08: * http://www.apache.org/licenses/LICENSE-2.0
09: *
10: * Unless required by applicable law or agreed to in writing, software
11: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13: * License for the specific language governing permissions and limitations under
14: * the License.
15: */
16: package com.google.gwt.dev.cfg;
17:
18: import com.google.gwt.core.ext.BadPropertyValueException;
19: import com.google.gwt.core.ext.PropertyOracle;
20: import com.google.gwt.core.ext.TreeLogger;
21:
22: /**
23: * An implementation of {@link PropertyOracle} that maintains explicit property
24: * values, rather than computing them.
25: */
26: public class StaticPropertyOracle implements PropertyOracle {
27:
28: private Property[] currentProps;
29:
30: private String[] currentValues;
31:
32: public StaticPropertyOracle() {
33: }
34:
35: public String getPropertyValue(TreeLogger logger,
36: String propertyName) throws BadPropertyValueException {
37: // In practice there will probably be so few properties that a linear
38: // search is at least as fast as a map lookup by name would be.
39: // If that turns out not to be the case, the ctor could build a
40: // name-to-index map.
41: //
42: for (int i = 0; i < currentProps.length; i++) {
43: Property prop = currentProps[i];
44: if (prop.getName().equals(propertyName)) {
45: String value = currentValues[i];
46: if (prop.isKnownValue(value)) {
47: return value;
48: } else {
49: throw new BadPropertyValueException(propertyName,
50: value);
51: }
52: }
53: }
54:
55: // Didn't find it.
56: //
57: throw new BadPropertyValueException(propertyName);
58: }
59:
60: public void setPropertyValues(Property[] props, String[] values) {
61: currentProps = props;
62: currentValues = values;
63: }
64: }
|