Example #1
0
 @Test
 public void testWithNullAsKeyAndValue() throws Exception {
   final T cache = getInstance(1000, 1000);
   assertEquals(0, (long) cache.size());
   assertFalse(cache.contains(null));
   assertNull(cache.get(null));
   // Add entry with null as key and value ...
   cache.put(null, null);
   assertEquals(1, (long) cache.size());
   assertTrue(cache.contains(null));
   assertNull(cache.get(null));
   // Add another entry ...
   cache.put(new Object(), new Object());
   assertEquals(2, (long) cache.size());
   // Remove entry with null as key ...
   assertNull(cache.remove(null).getValue());
   assertEquals(1, (long) cache.size());
   assertFalse(cache.contains(null));
   assertNull(cache.get(null));
   // Try to remove entry with null as key again ...
   assertNull(cache.remove(null));
   assertEquals(1, (long) cache.size());
   assertFalse(cache.contains(null));
   assertNull(cache.get(null));
 }
Example #2
0
 private static <T extends PersistentBase> T getPersistent(T persitent, String[] fields) {
   List<Schema.Field> otherFields = persitent.getSchema().getFields();
   String[] otherFieldStrings = new String[otherFields.size()];
   for (int i = 0; i < otherFields.size(); i++) {
     otherFieldStrings[i] = otherFields.get(i).name();
   }
   if (Arrays.equals(fields, otherFieldStrings)) {
     return persitent;
   }
   T clonedPersistent = AvroUtils.deepClonePersistent(persitent);
   clonedPersistent.clear();
   if (fields != null && fields.length > 0) {
     for (String field : fields) {
       Schema.Field otherField = persitent.getSchema().getField(field);
       int index = otherField.pos();
       clonedPersistent.put(index, persitent.get(index));
     }
   } else {
     for (String field : otherFieldStrings) {
       Schema.Field otherField = persitent.getSchema().getField(field);
       int index = otherField.pos();
       clonedPersistent.put(index, persitent.get(index));
     }
   }
   return clonedPersistent;
 }
Example #3
0
  @Override
  public void setParent(T parent) {
    // does the parent already contain an element with my name
    if (parent != null && parent.get(name) != null) {
      throw new IllegalArgumentException(
          this.toString()
              + ".setParent("
              + parent.toString()
              + ") failed: a element already exists with name = '"
              + name
              + "', being "
              + parent.get(name));
    }

    // oh, and if there are any keys that are in both maps that map to
    // different objects, fail
    for (Object ckey : this.treeElements.keySet()) {
      for (Object pkey : parent.getTreeElements().keySet()) {
        if (pkey.equals(ckey)
            && !parent.getTreeElements().get(pkey).equals(this.treeElements.get(ckey))) {
          throw new IllegalArgumentException(
              "setParent(" + parent.getName() + "): duplicate child '" + ckey + "'/'" + pkey + "'");
        }
      }
    }
    // should keep all children, so merge with parent map
    for (Object ckey : treeElements.keySet()) {
      if (!parent.getTreeElements().containsValue(treeElements.get(ckey))) {
        parent.getTreeElements().put(ckey, this.treeElements.get(ckey));
      }
    }
    // parent.treeElements.putAll(this.treeElements);//damn things copies
    treeElements = parent.getTreeElements();
    parentName = parent.getName();
  }
 @Override
 public double compare(T a_, T b_) {
   double dotProduct = 0.0;
   double normA = 0.0;
   double normB = 0.0;
   for (int i = 0; i < a_.size(); i++) {
     dotProduct += a_.get(i) * b_.get(i);
     normA += Math.pow(a_.get(i), 2);
     normB += Math.pow(b_.get(i), 2);
   }
   return (dotProduct / (Math.sqrt(normA) * Math.sqrt(normB)));
 }
  @Override
  public double valueOf(T t1, T t2) {
    double sum = 0;

    for (int i = 0; i < t1.size() && i < t2.size(); i++) {
      S s1 = t1.get(i);
      S s2 = t2.get(i);
      double d = kernel.valueOf(s1, s2);
      if (d > eps) sum += d;
    }

    return sum / ((double) Math.min(t1.size(), t2.size()));
  }
 protected <T extends ModelData> String extractValue(final T record) {
   final V propertyValue = record.<V>get(this.property);
   if (propertyValue == null) {
     return null;
   }
   return convertValue(propertyValue);
 }
Example #7
0
  /**
   * Construct a new Tree
   *
   * @param name unique name
   * @param parent the parent of this Tree. If null, then this is the root.
   */
  public SimpleTree(String name, T parent) {
    // System.out.println("SimpleTree name:" + name + "parent"+ parent);

    // checks
    if (StringUtils.isEmpty(name)) {
      throw new IllegalArgumentException("name cannot be empty");
    }
    if (parent != null)
      try {
        if (parent.get(name) != null)
          throw new IllegalArgumentException("elements already exists with name = '" + name + "'");
      } catch (NullPointerException e) {
        logger.error("NullPointer in constructor op SimpleTree", e);
      }

    // body
    this.name = name;
    if (parent == null) {
      // this is the root element of the tree, the map is ordered
      treeElements = new LinkedHashMap<String, T>();
    } else {
      treeElements = parent.getTreeElements(); // get a pointer to tree
      // elements.
      parentName = parent.getName();
    }
    treeElements.put(name, (T) this);
  }
  private void addQueryRule(T anchor, boolean afterwards, int pageSize, AVQuery<T> query) {

    // set page size
    query.limit(pageSize);
    // before or after

    String queryOrder = query.getOrder();
    if ((anchor != null) && (queryOrder != null)) {
      boolean isDescending = queryOrder.startsWith("-");
      String field = isDescending ? queryOrder.substring(1) : queryOrder;
      if ((isDescending && afterwards) || (!isDescending && !afterwards)) {
        query.whereLessThan(field, anchor.get(field));
      } else {
        query.whereGreaterThan(field, anchor.get(field));
      }
    }
  }
Example #9
0
 @Override
 public double get(int... indexes) {
   int d = indexes.length;
   T slice = slices[indexes[0]];
   switch (d) {
     case 0:
       throw new VectorzException("Can't do 0D get on SliceArray!");
     case 1:
       return slice.get();
     case 2:
       return slice.get(indexes[1]);
     case 3:
       return slice.get(indexes[1], indexes[2]);
     default:
       return slice.get(Arrays.copyOfRange(indexes, 1, d));
   }
 }
Example #10
0
 /**
  * Helper function to search a list of <code>ModelData</code> for a model with that has a value of
  * <code>id</code> for the property "id"
  *
  * @param list The list of <code>ModelData</code> to search
  * @param id The id for which to search
  * @param <T> The <code>ModelData</code> subclass
  * @return The corresponding <code>ModelData</code>, or null if none was found.
  */
 public static <T extends ModelData> T getById(List<T> list, int id) {
   for (T m : list) {
     Integer mId = m.get("id");
     if (mId != null && mId.equals(id)) {
       return m;
     }
   }
   return null;
 }
Example #11
0
  static <T extends Map> void shovMap(T o) {

    System.out.print("Convert Array to Map \nKey     ");
    for (Object ke : o.keySet()) {
      System.out.print(ke + "   ");
    }
    System.out.println();
    System.out.print("Value    ");
    for (Object s : o.keySet()) {
      System.out.print(o.get(s) + "     ");
    }
    System.out.println("\n\n");
  }
Example #12
0
 @Test
 public void testWithNullAsValue() throws Exception {
   final T cache = getInstance(1000, 1000);
   final Object o = new Object();
   assertEquals(0, (long) cache.size());
   assertFalse(cache.contains(o));
   assertNull(cache.get(o));
   // Add entry with null as value ...
   cache.put(o, null);
   assertEquals(1, (long) cache.size());
   assertTrue(cache.contains(o));
   assertNull(cache.get(o));
   // Remove entry with null as value ...
   assertNull(cache.remove(o).getValue());
   assertEquals(0, (long) cache.size());
   assertFalse(cache.contains(o));
   assertNull(cache.get(o));
   // Try to remove entry with null as value again ...
   assertNull(cache.remove(o));
   assertEquals(0, (long) cache.size());
   assertFalse(cache.contains(o));
   assertNull(cache.get(o));
 }
Example #13
0
  private <T extends WritableObjectValue<?>> boolean equalsProperty(T o1, T o2) {
    if (o1.get() != null && o2.get() != null) {
      if (o1.get() == o2.get()) {
        return true;
      }

      if (o1.get().equals(o2.get())) {
        return true;
      } else {
        return false;
      }
    } else {
      if (o1.get() == null && o2.get() == null) {
        return true;
      } else {
        return false;
      }
    }
  }
Example #14
0
 @Test
 public void testMaxLifeTimeSupport() throws Exception {
   final T cache = getInstance(100, 100);
   cache.put("varA", "valueA");
   cache.put("varB", "valueB");
   Thread.sleep(70);
   cache.put("varC", "valueC");
   assertEquals("valueA", cache.get("varA"));
   assertEquals("valueB", cache.get("varB"));
   assertEquals("valueC", cache.get("varC"));
   Thread.sleep(70);
   assertNull("valueA", cache.get("varA"));
   assertNull("valueB", cache.get("varB"));
   assertEquals("valueC", cache.get("varC"));
   Thread.sleep(70);
   assertNull("valueA", cache.get("varA"));
   assertNull("valueB", cache.get("varB"));
   assertNull("valueC", cache.get("varC"));
 }
Example #15
0
 public <T extends BooleanType<T>> BoolType(final T type) {
   this(type.get());
 }
 @Override
 protected int compareNotNullObjects(T left, T right) {
   return textComparator.compare(left.get(locale), right.get(locale));
 }
Example #17
0
 @Override
 public Element get(final Object key) {
   return copyElementForReadIfNeeded(store.get(key));
 }