예제 #1
0
  /**
   * Returns the PropertyTree associated with the given key. If none exists, creates a new
   * PropertyTree associates it with the given key, and returns it.
   *
   * @param key
   * @return PropertyTree associated with the given key
   * @throws IllegalArgumentException if the given key is null
   */
  private PropertyTree getChild(String key) {
    validateKey(key);

    PropertyTree child = (PropertyTree) this.children.get(key);
    if (child == null) {
      child = new PropertyTree((PropertyTree) this);
      this.children.put(key, child);
    }

    return child;
  }
예제 #2
0
  /** @see java.util.Map#containsKey(java.lang.Object) */
  public boolean containsKey(Object key) {
    validateKey(key);

    boolean containsKey = false;

    Set entrySet = entrySet();
    for (Iterator i = entrySet.iterator(); !containsKey && i.hasNext(); ) {
      Map.Entry e = (Map.Entry) i.next();

      Object entryKey = e.getKey();
      containsKey = (entryKey != null) && entryKey.equals(key);
    }

    return containsKey;
  }
예제 #3
0
  /**
   * Returns the PropertyTree object with the given key, or null if there is none.
   *
   * @param key
   * @return
   * @throws IllegalArgumentException if the key is null
   */
  private PropertyTree getSubtree(String key) {
    validateKey(key);

    PropertyTree returnValue = null;
    if (StringUtils.contains(key, '.')) {
      String prefix = StringUtils.substringBefore(key, ".");
      String suffix = StringUtils.substringAfter(key, ".");

      PropertyTree child = (PropertyTree) this.children.get(prefix);
      if (child != null) {
        returnValue = child.getSubtree(suffix);
      }
    } else {
      returnValue = (PropertyTree) this.children.get(key);
    }

    return returnValue;
  }
예제 #4
0
  /**
   * Associates the given key with the given value. If the given key has multiple levels (consists
   * of multiple strings separated by '.'), the property value is stored such that it can be
   * retrieved either directly, by calling get() and passing the entire key; or indirectly, by
   * decomposing the key into its separate levels and calling get() successively on the result of
   * the previous level's get. <br>
   * For example, given <br>
   * <code>
   * PropertyTree tree = new PropertyTree();
   * tree.set( "a.b.c", "something" );
   * </code> the following statements are equivalent ways to retrieve the value: <br>
   * <code>
   * Object one = tree.get( "a.b.c" );
   * </code> <code>
   * Object two = tree.get( "a" ).get( "b" ).get( "c" );
   * </code><br>
   * Note: since I can't have the get method return both a PropertyTree and a String, getting an
   * actual String requires calling toString on the PropertyTree returned by get.
   *
   * @param key
   * @param value
   * @throws IllegalArgumentException if the key is null
   * @throws IllegalArgumentException if the value is null
   */
  public void setProperty(String key, String value) {
    validateKey(key);
    validateValue(value);

    if (parent == null) {
      LOG.debug("setting (k,v) (" + key + "," + value + ")");
    }

    if (StringUtils.contains(key, '.')) {
      String prefix = StringUtils.substringBefore(key, ".");
      String suffix = StringUtils.substringAfter(key, ".");

      PropertyTree node = getChild(prefix);
      node.setProperty(suffix, value);
    } else {
      PropertyTree node = getChild(key);
      node.setDirectValue(value);
    }
  }
예제 #5
0
  /**
   * Traverses the tree structure until it finds the PropertyTree pointed to by the given key, and
   * returns that PropertyTree instance.
   *
   * <p>Only returns PropertyTree instances; if you want the String value pointed to by a given key,
   * you must call toString() on the returned PropertyTree (after verifying that it isn't null, of
   * course).
   *
   * @see java.util.Map#get(java.lang.Object)
   */
  public Object get(Object key) {
    validateKey(key);

    return getSubtree((String) key);
  }