All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.google.gwt.dev.cfg.BindingProperty Maven / Gradle / Ivy

Go to download

Vaadin is a web application framework for Rich Internet Applications (RIA). Vaadin enables easy development and maintenance of fast and secure rich web applications with a stunning look and feel and a wide browser support. It features a server-side architecture with the majority of the logic running on the server. Ajax technology is used at the browser-side to ensure a rich and interactive user experience.

There is a newer version: 8.25.2
Show newest version
/*
 * Copyright 2008 Google Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package com.google.gwt.dev.cfg;

import com.google.gwt.core.ext.linker.PropertyProviderGenerator;
import com.google.gwt.dev.util.collect.IdentityHashSet;
import com.google.gwt.dev.util.collect.Lists;
import com.google.gwt.dev.util.collect.Sets;
import com.google.gwt.thirdparty.guava.common.base.Objects;
import com.google.gwt.thirdparty.guava.common.collect.ImmutableList;
import com.google.gwt.thirdparty.guava.common.collect.ImmutableMap;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
import java.util.regex.Pattern;

/**
 * Represents a single named deferred binding or configuration property that can
 * answer with its value. The BindingProperty maintains two sets of values, the
 * "defined" set and the "allowed" set. The allowed set must always be a subset
 * of the defined set.
 */
public class BindingProperty extends Property {

  public static final String GLOB_STAR = "*";
  private static final String EMPTY = "";

  private List> collapsedValues = Lists.create();
  private final SortedSet definedValues = new TreeSet();
  private String fallback;
  private HashMap>> fallbackValueMap;
  private HashMap> fallbackValues = new HashMap>();
  private PropertyProvider provider;
  private Class providerGenerator;
  private final SortedSet targetLibraryDefinedValues = new TreeSet();
  private final ConditionAll rootCondition = new ConditionAll();

  /**
   * The binding values that are allowed for each condition.
   * (Used to determine what the properties were set to in the module file.)
   */
  private final ConditionalValues allowedValues = new ConditionalValues(rootCondition);

  /**
   * The binding values that we need to generate code for. (Affects the number of permutations.)
   * In a normal compile, this is the same as allowedValues, but in some compilation modes,
   * it's changed to restrict permutations. (For example, in Super Dev Mode.)
   */
  private final ConditionalValues generatedValues = new ConditionalValues(rootCondition);

  public BindingProperty(String name) {
    super(name);
    fallback = EMPTY;
  }

  /**
   * Add an equivalence set of property values.
   */
  public void addCollapsedValues(String... values) {

    // Sanity check caller
    for (String value : values) {
      if (value.contains(GLOB_STAR)) {
        // Expanded in normalizeCollapsedValues()
        continue;
      } else if (!definedValues.contains(value)) {
        throw new IllegalArgumentException(
            "Attempting to collapse unknown value " + value);
      }
    }

    // We want a mutable set, because it simplifies normalizeCollapsedValues
    SortedSet temp = new TreeSet(Arrays.asList(values));
    collapsedValues = Lists.add(collapsedValues, temp);
  }

  public void addDefinedValue(Condition condition, String definedValue) {
    definedValues.add(definedValue);
    allowedValues.addValue(condition, definedValue);
    generatedValues.addValue(condition, definedValue);
  }

  public void addTargetLibraryDefinedValue(Condition condition, String definedValue) {
    targetLibraryDefinedValues.add(definedValue);
    addDefinedValue(condition, definedValue);
  }

  /**
   * Adds fall back value to given property name.
   * @param value the property value.
   * @param fallbackValue the fall back value for given property value.
   */
  public void addFallbackValue(String value, String fallbackValue) {
    LinkedList values = fallbackValues.get(fallbackValue);
    if (values == null) {
      values = new LinkedList();
      fallbackValues.put(fallbackValue, values);
    }
    values.addFirst(value);
  }

  @Override
  public boolean equals(Object object) {
    if (object instanceof BindingProperty) {
      BindingProperty that = (BindingProperty) object;
      return Objects.equal(this.name, that.name)
          && Objects.equal(this.collapsedValues, that.collapsedValues)
          && Objects.equal(this.allowedValues, that.allowedValues)
          && Objects.equal(this.generatedValues, that.generatedValues)
          && Objects.equal(this.definedValues, that.definedValues)
          && Objects.equal(this.fallback, that.fallback)
          && Objects.equal(this.getFallbackValuesMap(), that.getFallbackValuesMap())
          && Objects.equal(this.fallbackValues, that.fallbackValues)
          && Objects.equal(this.provider, that.provider)
          && Objects.equal(this.providerGenerator, that.providerGenerator)
          && Objects.equal(this.rootCondition, that.rootCondition);
    }
    return false;
  }

  /**
   * Returns the set of values defined in the module file.
   * (For code generation, use {@link #getGeneratedValues} because this might be
   * overridden.)
   */
  public String[] getAllowedValues(Condition condition) {
    return allowedValues.getValuesAsArray(condition);
  }

  /**
   * Returns the set of values for which the GWT compiler must generate permutations.
   */
  public String[] getGeneratedValues(Condition condition) {
    return generatedValues.getValuesAsArray(condition);
  }

  public List> getCollapsedValues() {
    return collapsedValues;
  }

  /**
   * Returns a map containing the generated values for each condition, in the order
   * they were added to the module files.
   */
  public ImmutableMap> getConditionalValues() {
    return generatedValues.toMap();
  }

  /**
   * If the BindingProperty has exactly one generated value across all conditions and
   * permutations, return that value otherwise return null.
   */
  public String getConstrainedValue() {
    if (!generatedValues.allConditionsHaveOneValue()) {
      return null;
    }
    Set values = generatedValues.getAllValues();
    if (values.size() != 1) {
      return null; // For example, two conditions could each have a different value.
    }
    return values.iterator().next();
  }

  /**
   * Returns the set of defined values in sorted order.
   */
  public String[] getDefinedValues() {
    return definedValues.toArray(new String[definedValues.size()]);
  }

  /**
   * Returns the fallback value for this property, or the empty string if none.
   *
   * @return the fallback value
   */
  public String getFallback() {
    return fallback;
  }

  /**
   * Returns the map of values to fall back values. the list of fall
   * back values is in decreasing order of preference.
   * @return map of property value to fall back values.
   */
  public Map>> getFallbackValuesMap() {
    if (fallbackValueMap == null) {
      HashMap>> valuesMap = new HashMap>>();
      // compute closure of fall back values preserving order
      for (Entry> e : fallbackValues.entrySet()) {
        String from = e.getKey();
        LinkedList> alternates = new LinkedList>();
        valuesMap.put(from, alternates);
        LinkedList childList = fallbackValues.get(from);
        LinkedHashSet children = new LinkedHashSet();
        children.addAll(childList);
        while (children != null && children.size() > 0) {
          alternates.add(children);
          LinkedHashSet newChildren = new LinkedHashSet();
          for (String child : children) {
            childList = fallbackValues.get(child);
            if (null == childList) {
              continue;
            }
            for (String val : childList) {
              newChildren.add(val);
            }
          }
          children = newChildren;
        }
      }
      fallbackValueMap = valuesMap;
    }
    return fallbackValueMap;
  }

  /**
   * Returns the first value from the list of defined values that is that is also allowed.
   * @throws IllegalStateException if there is not at least one value that's both defined
   * and allowed.
   */
  public String getFirstAllowedValue() {
    String value = allowedValues.getFirstMember(definedValues);
    if (value == null) {
      throw new IllegalStateException("binding property has no allowed values: " + name);
    }
    return value;
  }

  /**
   * Returns the first value from the list of defined values that is actually generated.
   * @throws IllegalStateException if there is not at least one value that's both defined
   * and generated.
   */
  public String getFirstGeneratedValue() {
    if (definedValues.isEmpty()) {
      // This shouldn't happen but a DynamicPropertyOracleTest currently requires it.
      // TODO(skybrian) we should probably require fallback values to be defined.
      // (It's checked when parsing the XML.)
      return fallback;
    }
    String value = generatedValues.getFirstMember(definedValues);
    if (value == null) {
      throw new IllegalStateException("binding property has no generated values: " + name);
    }
    return value;
  }

  public List getTargetLibraryDefinedValues() {
    return ImmutableList.copyOf(targetLibraryDefinedValues);
  }

  public PropertyProvider getProvider() {
    return provider;
  }

  /**
   * @return the provider generator class, or null if none.
   */
  public Class getProviderGenerator() {
    return providerGenerator;
  }

  public Set getRequiredProperties() {
    Set toReturn = Sets.create();
    for (Condition cond : generatedValues.eachCondition()) {
      toReturn = Sets.addAll(toReturn, cond.getRequiredProperties());
    }
    return toReturn;
  }

  public ConditionAll getRootCondition() {
    return rootCondition;
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(name, collapsedValues, allowedValues, definedValues, fallback,
        getFallbackValuesMap(), fallbackValues, provider, providerGenerator, rootCondition);
  }

  /**
   * Returns true if the value was previously provided to
   * {@link #addDefinedValue(Condition,String)}.
   */
  public boolean isDefinedValue(String value) {
    return definedValues.contains(value);
  }

  /**
   * Returns true if the supplied value is used based on the module file.
   */
  public boolean isAllowedValue(String value) {
    return allowedValues.containsValue(value);
  }

  /**
   * Returns true if the supplied value will be used during code generation.
   */
  public boolean isGeneratedValue(String value) {
    return generatedValues.containsValue(value);
  }

  /**
   * Returns true if the value of this BindingProperty is always
   * derived from other BindingProperties. That is, for each Condition in the
   * BindingProperty, there is exactly one generated value.
   */
  public boolean isDerived() {
    return generatedValues.allConditionsHaveOneValue();
  }

  public void setFallback(String token) {
    fallback = token;
  }

  public void setProvider(PropertyProvider provider) {
    this.provider = provider;
  }

  /**
   * Set a provider generator for this property.
   */
  public void setProviderGenerator(Class generator) {
    providerGenerator = generator;
  }

  /**
   * Create a minimal number of equivalence sets, expanding any glob patterns.
   */
  void normalizeCollapsedValues() {
    if (collapsedValues.isEmpty()) {
      return;
    }

    // Expand globs
    for (Set set : collapsedValues) {
      // Compile a regex that matches all glob expressions that we see
      StringBuilder pattern = new StringBuilder();
      for (Iterator it = set.iterator(); it.hasNext();) {
        String value = it.next();
        if (value.contains(GLOB_STAR)) {
          it.remove();
          if (pattern.length() > 0) {
            pattern.append("|");
          }

          // a*b ==> (a.*b)
          pattern.append("(");
          // We know value is a Java ident, so no special escaping is needed
          pattern.append(value.replace(GLOB_STAR, ".*"));
          pattern.append(")");
        }
      }

      if (pattern.length() == 0) {
        continue;
      }

      Pattern p = Pattern.compile(pattern.toString());
      for (String definedValue : definedValues) {
        if (p.matcher(definedValue).matches()) {
          set.add(definedValue);
        }
      }
    }

    // Minimize number of sets

    // Maps a value to the set that contains that value
    Map> map = new HashMap>();

    // For each equivalence set we have
    for (SortedSet set : collapsedValues) {
      // Examine each original value in the set
      for (String value : new LinkedHashSet(set)) {
        // See if the value was previously assigned to another set
        SortedSet existing = map.get(value);
        if (existing == null) {
          map.put(value, set);
        } else {
          // If so, merge the existing set into this one and update pointers
          set.addAll(existing);
          for (String mergedValue : existing) {
            map.put(mergedValue, set);
          }
        }
      }
    }

    // The values of the maps will now contain the minimal number of sets
    collapsedValues = new ArrayList>(
        new IdentityHashSet>(map.values()));

    // Sort the list
    Lists.sort(collapsedValues, new Comparator>() {
      @Override
      public int compare(SortedSet o1, SortedSet o2) {
        String s1 = o1.toString();
        String s2 = o2.toString();
        assert !s1.equals(s2) : "Should not have seen equal sets";
        return s1.compareTo(s2);
      }
    });
  }

  /**
   * Replaces the allowed and generated values for a condition.
   * If it is the root condition, clears all other conditions.
   *
   * @throws IllegalArgumentException if any value isn't currently defined.
   */
  public void setValues(Condition bindingPropertyCondition, String... values) {
    List valueList = Arrays.asList(values);
    checkAllDefined(valueList);
    allowedValues.putValues(bindingPropertyCondition, valueList);
    generatedValues.putValues(bindingPropertyCondition, valueList);
  }

  /**
   * Overrides the generated values for the root condition and clears the
   * generated values for other conditions.
   *
   * This has no effect on the allowed values and ignores them.
   * It's intended for artificially restricting permutations
   * in special modes like Super Dev Mode and the GWTTestCase runner.
   *
   * @throws IllegalArgumentException if any value isn't currently defined.
   */
  public void setRootGeneratedValues(String... values) {
    List valueList = Arrays.asList(values);
    checkAllDefined(valueList);
    generatedValues.replaceAllValues(valueList);
  }

  private void checkAllDefined(Collection valueList) {
    for (String value : valueList) {
      if (!definedValues.contains(value)) {
        throw new IllegalArgumentException(
            "Attempted to set a binding property to a value that was not previously defined: " +
                name + " = '" + value + "'");
      }
    }
  }

  /**
   * Contains a set of binding values for each condition.
   *
   * 

Remembers the order in which they were added. This is needed because * the order in which properties were set in GWT module files is significant. * (The last one wins.) */ private static class ConditionalValues implements Serializable { private final Condition root; private final Map> valueMap = new LinkedHashMap>(); private ConditionalValues(Condition root) { // The root condition always has a set of values. By default this is empty. this.root = root; valueMap.put(root, new TreeSet()); } /** * Adds one more value under a condition. If there isn't any set of values for * the given condition, creates it by copying the root condition. */ private void addValue(Condition condition, String value) { SortedSet set = valueMap.get(condition); if (set == null) { set = new TreeSet(valueMap.get(root)); valueMap.put(condition, set); } set.add(value); } /** * Replaces all the values for a condition and moves it to the end of the map. * If it is the root condition, also clears the other conditions. */ private void putValues(Condition condition, Collection values) { // XML has a last-one-wins semantic which we reflect in our evaluation order if (condition == root) { // An unconditional set-property would undo any previous conditional // setters, so we can just clear out this map. replaceAllValues(values); } else { // Otherwise, we'll just ensure that this condition is moved to the end. valueMap.remove(condition); valueMap.put(condition, new TreeSet(values)); } } private void replaceAllValues(Collection rootValues) { valueMap.clear(); valueMap.put(root, new TreeSet(rootValues)); } // === queries === private ImmutableMap> toMap() { return ImmutableMap.copyOf(valueMap); } private String[] getValuesAsArray(Condition condition) { Set values = valueMap.get(condition); return values.toArray(new String[values.size()]); } private Iterable eachCondition() { return valueMap.keySet(); } /** * Returns true if the value appears for any condition. */ private boolean containsValue(String value) { for (Set values : valueMap.values()) { if (values.contains(value)) { return true; } } return false; } /** * Returns all values that appear under at least one condition. */ private Set getAllValues() { SortedSet result = new TreeSet(); for (SortedSet valueSet : valueMap.values()) { result.addAll(valueSet); } return result; } /** * Returns the first value from the given list that's used in at least one condition, * or null if none are. */ private String getFirstMember(Iterable candidates) { Set members = getAllValues(); for (String candidate : candidates) { if (members.contains(candidate)) { return candidate; } } return null; } private boolean allConditionsHaveOneValue() { for (Set values : valueMap.values()) { if (values.size() != 1) { return false; } } return true; } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy