Пример #1
0
  /**
   * Stores this object in the provided Map. In case of an indexed key use _name to get a
   * corresponding List, and _index for the element in the list. In case of a simple key just put
   * the value at the name key
   *
   * <p>No overrides are allowed.
   *
   * @param map the map to store the value in
   * @param value the value to put in the map
   * @throws PathSegmentSyntaxException if the object is unable to be placed
   */
  public void putOnDataMap(MapMap map, String value) throws PathSegmentSyntaxException {
    // If not an indexed key, just store the value in the provided Map
    // with the current _name as key. If already there - error
    if (_index == null) {
      if (map.get(_name) != null)
        throw new PathSegmentSyntaxException("Duplicate references to key: " + _name);

      map.put(_name, value);
      return;
    }

    // Otherwise, get the element at the key _name, which is assumed to be
    // a Map keyed on Integer (as opposed to String for DataMaps), and store
    // the value at the index key in that map.
    ListMap listMap;
    Object entry = map.get(_name);
    if (entry == null) {
      listMap = new ListMap();
      map.put(_name, listMap);
    } else if (entry instanceof ListMap) {
      listMap = (ListMap) entry;
    } else {
      throw new PathSegmentSyntaxException("Conflicting references to key: " + toString());
    }

    // Now put the value on the _index entry of the list if it's not there.
    // If it is - exception
    if (listMap.get(_index) != null)
      throw new PathSegmentSyntaxException("Duplicate references to key: " + _name);

    listMap.put(_index, value);
  }
Пример #2
0
  /**
   * Get the value referenced by this path segment in the provided DataMap
   *
   * <p>The method assumes the current key is not a leaf segment of the key path, i.e. it always
   * references a Map value in the current Map.
   *
   * @param map the Map
   * @return a Map of Strings to Maps
   * @throws PathSegmentSyntaxException if the current key is a leaf segment
   */
  public MapMap getNextLevelMap(MapMap map) throws PathSegmentSyntaxException {
    Object object = map.get(_name);

    if (object == null) {
      MapMap nextLevelDataMap = new MapMap();
      // If not an indexed path segment, create a new Map, put it at the
      // current key and return it.
      if (_index == null) {
        map.put(_name, nextLevelDataMap);
      }
      // If an indexed key, create a new ListMap, put it at the current _name
      // and put next level data map at the current _index in the List Map
      else {
        ListMap listMap = new ListMap();
        map.put(_name, listMap);
        listMap.put(_index, nextLevelDataMap);
      }
      return nextLevelDataMap;
    }

    if (_index == null) {
      if (object instanceof MapMap) return (MapMap) object;
      throw new PathSegmentSyntaxException("Conflicting references to key " + toString());
    }

    if (object instanceof ListMap) {
      ListMap list = (ListMap) object;

      Object object2 = list.get(_index);

      if (object2 == null) {
        MapMap nextLevelMap = new MapMap();
        list.put(_index, nextLevelMap);
        return nextLevelMap;
      } else if (object2 instanceof MapMap) {
        return (MapMap) object2;
      } else {
        throw new PathSegmentSyntaxException("Conflicting references to key " + toString());
      }
    }

    throw new PathSegmentSyntaxException("Conflicting references to key " + toString());
  }