@SuppressWarnings("unchecked")
  public static <T1 extends Model, T2 extends Model> List<T2> getRelations(
      Class<? extends OneToManyRelation<T1, T2>> relation, T1 entity) {
    if (entity.getId() == null)
      throw new IllegalArgumentException(
          entity.getClass().getSimpleName() + " is not saved to database yet, aborting");

    TableInfo crossTableInfo = Cache.getTableInfo(relation);
    Cursor cursor =
        Cache.openDatabase()
            .rawQuery(
                "SELECT entity2Type, entity2 FROM "
                    + crossTableInfo.getTableName()
                    + " WHERE entity1 = ?",
                new String[] {entity.getId().toString()});
    final List<T2> entities = new ArrayList<T2>();
    try {
      if (cursor.moveToFirst()) {
        do {
          String typeName = cursor.getString(0);
          Class<? extends Model> entity2Class = (Class<? extends Model>) Class.forName(typeName);
          entities.add((T2) Model.load(entity2Class, cursor.getLong(1)));
        } while (cursor.moveToNext());
      }
    } catch (Exception e) {
      Log.e("Failed to process cursor.", e);
      throw new RuntimeException(e);
    } finally {
      cursor.close();
    }

    return entities;
  }
Ejemplo n.º 2
0
 public boolean equals(Object other) {
   Pair rhs = (Pair) other;
   if (first == null && second == null) return rhs.first == null && rhs.second == null;
   if (first == null) return rhs.first == null && second.equals(rhs.second);
   if (second == null) return rhs.second == null && first.equals(rhs.first);
   return first.equals(rhs.first) && second.equals(rhs.second);
 }
  public static <T1 extends Model, T2 extends Model> void setRelations(
      Class<? extends OneToManyRelation<T1, T2>> relation, T1 entity1, List<T2> entities2) {
    if (entity1.getId() == null)
      throw new IllegalArgumentException(
          entity1.getClass().getSimpleName() + " is not saved to database yet, aborting");
    for (T2 entity2 : entities2) {
      if (entity2.getId() == null)
        throw new IllegalArgumentException(
            entity2.getClass().getSimpleName() + " is not saved to database yet, aborting");
    }

    new Delete().from(relation).where("entity1 = ?", entity1.getId()).execute();
    try {
      List<OneToManyRelation<T1, T2>> connections = new ArrayList<OneToManyRelation<T1, T2>>();
      for (T2 entity2 : entities2) {
        OneToManyRelation<T1, T2> connection = relation.newInstance();
        connection.entity1 = entity1;
        connection.entity2Type = entity2.getClass().getCanonicalName();
        connection.entity2 = entity2;
        connections.add(connection);
      }
      saveMultiple(connections);
    } catch (Exception e) {
      Log.e("Cannot create instance of class " + relation.getSimpleName());
      throw new RuntimeException(e);
    }
  }
Ejemplo n.º 4
0
 protected <T1 extends Named> T1 getByName(List<T1> list, String name) {
   for (T1 t1 : list) {
     if (t1.getName().equals(name)) {
       return t1;
     }
   }
   return null;
 }
Ejemplo n.º 5
0
 protected <T1 extends identified> T1 getById(List<T1> list, Integer id) {
   for (T1 t1 : list) {
     if (t1.getId().equals(id)) {
       return t1;
     }
   }
   return null;
 }
Ejemplo n.º 6
0
 @Override
 public void readFields(DataInput in) throws IOException {
   llist.clear();
   rlist.clear();
   int size = in.readInt();
   for (int i = 0; i < size; i++) {
     T1 e1 = createLValue();
     e1.readFields(in);
     T2 e2 = createRValue();
     e2.readFields(in);
     llist.add(e1);
     rlist.add(e2);
   }
 }
Ejemplo n.º 7
0
 @Override
 public int hashCode() {
   // Null hashes arbitrarily chosen by keyboard mashing.
   int firsthash = first == null ? 5960343 : first.hashCode();
   int secondhash = second == null ? 1186323 : second.hashCode();
   return firsthash ^ Integer.rotateLeft(secondhash, 13);
 }
Ejemplo n.º 8
0
 public int compareTo(Par<T1, T2> o) {
   int cmp = val1.compareTo(o.val1);
   if (cmp == 0) {
     return val2.compareTo(o.val2);
   }
   return cmp;
 }
Ejemplo n.º 9
0
 @Override
 public boolean equals(Object obj) {
   if (this == obj) {
     return true;
   }
   if (obj == null) {
     return false;
   }
   if (getClass() != obj.getClass()) {
     return false;
   }
   CltPair<?, ?> other = (CltPair<?, ?>) obj;
   if (left == null) {
     if (other.left != null) {
       return false;
     }
   } else if (!left.equals(other.left)) {
     return false;
   }
   if (right == null) {
     if (other.right != null) {
       return false;
     }
   } else if (!right.equals(other.right)) {
     return false;
   }
   return true;
 }
  @Override
  public String toString() {
    StringBuilder returnString = new StringBuilder("<");

    if (value1 != null) {
      returnString.append(value1.toString());
    } else {
      returnString.append("<null>");
    }

    returnString.append(", ");

    if (value2 != null) {
      returnString.append(value2.toString());
    } else {
      returnString.append("<null>");
    }

    returnString.append(", ");

    if (value3 != null) {
      returnString.append(value3.toString());
    } else {
      returnString.append("<null>");
    }

    returnString.append(">");
    return returnString.toString();
  }
Ejemplo n.º 11
0
 @Override
 public int hashCode() {
   final int prime = 31;
   int result = 1;
   result = prime * result + ((value1 == null) ? 0 : value1.hashCode());
   result = prime * result + ((value2 == null) ? 0 : value2.hashCode());
   return result;
 }
Ejemplo n.º 12
0
 /**
  * Write a string representation of a Pair from a DataStream. The <code>toString()</code> method
  * is called on each of the pair of objects and a <code>String</code> representation is written.
  * This might not allow one to recover the pair of objects unless they are of type <code>String
  * </code>.
  */
 public void save(DataOutputStream out) {
   try {
     out.writeUTF(first.toString());
     out.writeUTF(second.toString());
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Ejemplo n.º 13
0
 @Override
 public int hashCode() {
   final int prime = 31;
   int result = 1;
   result = prime * result + ((left == null) ? 0 : left.hashCode());
   result = prime * result + ((right == null) ? 0 : right.hashCode());
   return result;
 }
Ejemplo n.º 14
0
    @Override
    public boolean equals(Object obj) {
      if (!(obj instanceof Pair)) return false;

      Pair<T1, T2> other = (Pair<T1, T2>) obj;

      return (first == null ? other.first == null : first.equals(other.first))
          && (second == null ? other.second == null : second.equals(other.second));
    }
Ejemplo n.º 15
0
 public boolean equals(Object o) {
   if (o instanceof Pair) {
     Pair p = (Pair) o;
     return (first == null ? p.first == null : first.equals(p.first))
         && (second == null ? p.second == null : second.equals(p.second));
   } else {
     return false;
   }
 }
Ejemplo n.º 16
0
 @Override
 public int hashCode() {
   final int prime = 31;
   int result = 1;
   result = prime * result + ((first == null) ? 0 : first.hashCode());
   result = prime * result + ((second == null) ? 0 : second.hashCode());
   result = prime * result + ((third == null) ? 0 : third.hashCode());
   return result;
 }
Ejemplo n.º 17
0
 public int hashCode() {
   int c = 1;
   if (o1 != null) {
     c = o1.hashCode();
   }
   if (o2 != null) {
     c = 31 * c + o2.hashCode();
   }
   return c;
 }
Ejemplo n.º 18
0
 /*
  * (non-Javadoc)
  *
  * @see java.lang.Object#equals(java.lang.Object)
  */
 @Override
 public boolean equals(Object obj) {
   if (obj instanceof Pair) {
     Pair<?, ?> o = (Pair<?, ?>) obj;
     try {
       return first.equals(o.first) && second.equals(o.second);
     } catch (NullPointerException e) {
       if (first == null && o.first == null) {
         if (second == null && o.second == null) return true;
         else if (second != null) return second.equals(o.second);
       } else if (first != null && first.equals(o.first)) {
         if (second == null && o.second == null) return true;
         else if (second != null) return second.equals(o.second);
       }
       return false;
     }
   }
   return false;
 }
Ejemplo n.º 19
0
 @Override
 public boolean equals(Object o) {
   if (o instanceof Pair) {
     @SuppressWarnings("rawtypes")
     Pair p = (Pair) o;
     return (first == null ? p.first() == null : first.equals(p.first()))
         && (second == null ? p.second() == null : second.equals(p.second()));
   } else {
     return false;
   }
 }
  @Override
  public boolean equals(Object obj) {
    if (obj == null) {
      return false;
    }
    if (getClass() != obj.getClass()) {
      return false;
    } else {
      TwoTypePair otherPair = (TwoTypePair) obj;
      return (first.equals(otherPair.first)) && second.equals(otherPair.second);
    }

    //        final TwoTypePair<?, ?> other = (TwoTypePair<?, ?>) obj;
    //        if (!Objects.equals(this.first, other.first)) {
    //            return false;
    //        }
    //        if (!Objects.equals(this.second, other.second)) {
    //            return false;
    //        }
    //        return true;
  }
  /** @throws Exception */
  @Test
  public void testBlocking() throws Exception {
    final PageAccessSynchronizer sync = new PageAccessSynchronizer(Duration.seconds(5));
    final Duration hold = Duration.seconds(1);
    final Time t1locks[] = new Time[1];
    final Time t2locks[] = new Time[1];

    class T1 extends Thread {
      @Override
      public void run() {
        sync.lockPage(1);
        t1locks[0] = Time.now();
        hold.sleep();
        sync.unlockAllPages();
      }
    }

    class T2 extends Thread {
      @Override
      public void run() {
        sync.lockPage(1);
        t2locks[0] = Time.now();
        sync.unlockAllPages();
      }
    }

    T1 t1 = new T1();
    t1.setName("t1");
    T2 t2 = new T2();
    t2.setName("t2");
    t1.start();
    Duration.milliseconds(100).sleep();
    t2.start();

    t1.join();
    t2.join();

    assertTrue(!t2locks[0].before(t1locks[0].add(hold)));
  }
Ejemplo n.º 22
0
 @Override
 public boolean equals(Object obj) {
   if (this == obj) return true;
   if (obj == null) return false;
   if (getClass() != obj.getClass()) return false;
   Tuple2<?, ?> other = (Tuple2<?, ?>) obj;
   if (value1 == null) {
     if (other.value1 != null) return false;
   } else if (!value1.equals(other.value1)) return false;
   if (value2 == null) {
     if (other.value2 != null) return false;
   } else if (!value2.equals(other.value2)) return false;
   return true;
 }
Ejemplo n.º 23
0
  @Override
  public int hashCode() {
    final int prime = 31;
    int result = 1;

    result = prime * result + ((v1 == null) ? 0 : v1.hashCode());
    result = prime * result + ((v2 == null) ? 0 : v2.hashCode());
    result = prime * result + ((v3 == null) ? 0 : v3.hashCode());
    result = prime * result + ((v4 == null) ? 0 : v4.hashCode());
    result = prime * result + ((v5 == null) ? 0 : v5.hashCode());
    result = prime * result + ((v6 == null) ? 0 : v6.hashCode());
    result = prime * result + ((v7 == null) ? 0 : v7.hashCode());
    result = prime * result + ((v8 == null) ? 0 : v8.hashCode());

    return result;
  }
Ejemplo n.º 24
0
  @Override
  public final boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }

    Key2 key2 = (Key2) o;

    if (!value1.equals(key2.value1)) {
      return false;
    }
    if (!value2.equals(key2.value2)) {
      return false;
    }

    return true;
  }
Ejemplo n.º 25
0
 @Override
 public int hashCode() {
   int result = f0 != null ? f0.hashCode() : 0;
   result = 31 * result + (f1 != null ? f1.hashCode() : 0);
   result = 31 * result + (f2 != null ? f2.hashCode() : 0);
   result = 31 * result + (f3 != null ? f3.hashCode() : 0);
   result = 31 * result + (f4 != null ? f4.hashCode() : 0);
   result = 31 * result + (f5 != null ? f5.hashCode() : 0);
   result = 31 * result + (f6 != null ? f6.hashCode() : 0);
   result = 31 * result + (f7 != null ? f7.hashCode() : 0);
   result = 31 * result + (f8 != null ? f8.hashCode() : 0);
   result = 31 * result + (f9 != null ? f9.hashCode() : 0);
   result = 31 * result + (f10 != null ? f10.hashCode() : 0);
   result = 31 * result + (f11 != null ? f11.hashCode() : 0);
   result = 31 * result + (f12 != null ? f12.hashCode() : 0);
   result = 31 * result + (f13 != null ? f13.hashCode() : 0);
   result = 31 * result + (f14 != null ? f14.hashCode() : 0);
   result = 31 * result + (f15 != null ? f15.hashCode() : 0);
   result = 31 * result + (f16 != null ? f16.hashCode() : 0);
   result = 31 * result + (f17 != null ? f17.hashCode() : 0);
   return result;
 }
Ejemplo n.º 26
0
 @Override
 public int hashCode() {
   return val1.hashCode() ^ val2.hashCode();
 }
Ejemplo n.º 27
0
 /*
  * (non-Javadoc)
  *
  * @see java.lang.Object#hashCode()
  */
 @Override
 public int hashCode() {
   return (first != null ? first.hashCode() : 0) * (second != null ? second.hashCode() : 0);
 }
Ejemplo n.º 28
0
 @Override
 public String toString() {
   return e1.toString() + "#" + e2.toString();
 }
Ejemplo n.º 29
0
 /**
  * Deep equality for tuples by calling equals() on the tuple members
  *
  * @param o the object checked for equality
  * @return true if this is equal to o.
  */
 @Override
 public boolean equals(Object o) {
   if (this == o) {
     return true;
   }
   if (!(o instanceof Tuple18)) {
     return false;
   }
   @SuppressWarnings("rawtypes")
   Tuple18 tuple = (Tuple18) o;
   if (f0 != null ? !f0.equals(tuple.f0) : tuple.f0 != null) {
     return false;
   }
   if (f1 != null ? !f1.equals(tuple.f1) : tuple.f1 != null) {
     return false;
   }
   if (f2 != null ? !f2.equals(tuple.f2) : tuple.f2 != null) {
     return false;
   }
   if (f3 != null ? !f3.equals(tuple.f3) : tuple.f3 != null) {
     return false;
   }
   if (f4 != null ? !f4.equals(tuple.f4) : tuple.f4 != null) {
     return false;
   }
   if (f5 != null ? !f5.equals(tuple.f5) : tuple.f5 != null) {
     return false;
   }
   if (f6 != null ? !f6.equals(tuple.f6) : tuple.f6 != null) {
     return false;
   }
   if (f7 != null ? !f7.equals(tuple.f7) : tuple.f7 != null) {
     return false;
   }
   if (f8 != null ? !f8.equals(tuple.f8) : tuple.f8 != null) {
     return false;
   }
   if (f9 != null ? !f9.equals(tuple.f9) : tuple.f9 != null) {
     return false;
   }
   if (f10 != null ? !f10.equals(tuple.f10) : tuple.f10 != null) {
     return false;
   }
   if (f11 != null ? !f11.equals(tuple.f11) : tuple.f11 != null) {
     return false;
   }
   if (f12 != null ? !f12.equals(tuple.f12) : tuple.f12 != null) {
     return false;
   }
   if (f13 != null ? !f13.equals(tuple.f13) : tuple.f13 != null) {
     return false;
   }
   if (f14 != null ? !f14.equals(tuple.f14) : tuple.f14 != null) {
     return false;
   }
   if (f15 != null ? !f15.equals(tuple.f15) : tuple.f15 != null) {
     return false;
   }
   if (f16 != null ? !f16.equals(tuple.f16) : tuple.f16 != null) {
     return false;
   }
   if (f17 != null ? !f17.equals(tuple.f17) : tuple.f17 != null) {
     return false;
   }
   return true;
 }
Ejemplo n.º 30
0
 @Override
 public int hashCode() {
   return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
 }