Exemple #1
0
  public static <K extends Comparable<K>, V> Map<K, V> convertToMap(
      final List<OPair<K, V>> iValues) {
    final HashMap<K, V> result = new HashMap<K, V>(iValues.size());
    for (OPair<K, V> p : iValues) result.put(p.getKey(), p.getValue());

    return result;
  }
  /**
   * (Blueprints Extension) Sets multiple properties in one shot against Vertices and Edges without
   * saving the element. This improves performance avoiding to save the graph element at every
   * property set. Example: <code>
   * vertex.setProperties( "name", "Jill", "age", 33, "city", "Rome", "born", "Victoria, TX" );
   * </code> You can also pass a Map of values as first argument. In this case all the map entries
   * will be set as element properties: <code>
   * Map<String,Object> props = new HashMap<String,Object>();
   * props.put("name", "Jill");
   * props.put("age", 33);
   * props.put("city", "Rome");
   * props.put("born", "Victoria, TX");
   * vertex.setProperties(props);
   * </code>
   *
   * @param fields Odd number of fields to set as repeating pairs of key, value, or if one parameter
   *     is received and it's a Map, the Map entries are used as field key/value pairs.
   * @param <T>
   * @return
   */
  protected <T extends OrientElement> T setPropertiesInternal(final Object... fields) {
    OrientBaseGraph graph = getGraph();
    if (fields != null && fields.length > 0 && fields[0] != null) {
      if (graph != null) graph.autoStartTransaction();

      if (fields.length == 1) {
        Object f = fields[0];
        if (f instanceof Map<?, ?>) {
          for (Map.Entry<Object, Object> entry : ((Map<Object, Object>) f).entrySet())
            setPropertyInternal(
                this,
                (ODocument) rawElement.getRecord(),
                entry.getKey().toString(),
                entry.getValue());

        } else if (f instanceof Collection) {
          for (Object o : (Collection) f) {
            if (!(o instanceof OPair))
              throw new IllegalArgumentException(
                  "Invalid fields: expecting a pairs of fields as String,Object, but found the item: "
                      + o);

            final OPair entry = (OPair) o;
            setPropertyInternal(
                this,
                (ODocument) rawElement.getRecord(),
                entry.getKey().toString(),
                entry.getValue());
          }

        } else
          throw new IllegalArgumentException(
              "Invalid fields: expecting a pairs of fields as String,Object or a single Map<String,Object>, but found: "
                  + f);
      } else {
        if (fields.length % 2 != 0)
          throw new IllegalArgumentException(
              "Invalid fields: expecting a pairs of fields as String,Object or a single Map<String,Object>, but found: "
                  + Arrays.toString(fields));

        // SET THE FIELDS
        for (int i = 0; i < fields.length; i += 2)
          setPropertyInternal(
              this, (ODocument) rawElement.getRecord(), fields[i].toString(), fields[i + 1]);
      }
    }
    return (T) this;
  }