Author: [log in to unmask] Date: Tue Apr 14 18:59:28 2015 New Revision: 2699 Log: Add copy constructor. Modified: java/trunk/conditions/src/main/java/org/hps/conditions/api/ConditionsRecord.java Modified: java/trunk/conditions/src/main/java/org/hps/conditions/api/ConditionsRecord.java ============================================================================= --- java/trunk/conditions/src/main/java/org/hps/conditions/api/ConditionsRecord.java (original) +++ java/trunk/conditions/src/main/java/org/hps/conditions/api/ConditionsRecord.java Tue Apr 14 18:59:28 2015 @@ -22,7 +22,7 @@ * * @author <a href="mailto:[log in to unmask]">Jeremy McCormick</a> */ -@Table(names = { "conditions" }) +@Table(names = {"conditions"}) @Converter(converter = ConditionsRecordConverter.class) public final class ConditionsRecord extends BaseConditionsObject { @@ -33,106 +33,45 @@ public static class ConditionsRecordCollection extends BaseConditionsObjectCollection<ConditionsRecord> { /** - * Sort using a comparator and leave the original collection unchanged. - * - * @param comparator the comparison to use for sorting - * @return the sorted collection - */ - public final ConditionsRecordCollection sorted(final Comparator<ConditionsRecord> comparator) { - final List<ConditionsRecord> list = new ArrayList<ConditionsRecord>(this); - Collections.sort(list, comparator); - final ConditionsRecordCollection collection = new ConditionsRecordCollection(); - collection.addAll(list); - return collection; - } - - /** - * Sort and return collection by updated date. - * - * @return the sorted collection - */ - public final ConditionsRecordCollection sortedByUpdated() { - return sorted(new UpdatedComparator()); - } - - /** - * Sort and return collection by creation date. - * - * @return the sorted collection - */ - public final ConditionsRecordCollection sortedByCreated() { - return sorted(new CreatedComparator()); - } - - /** - * Sort and return by run start number. - * - * @return the sorted collection - */ - public final ConditionsRecordCollection sortedByRunStart() { - return sorted(new RunStartComparator()); - } - - /** - * Sort and return by key (table name). - * - * @return the sorted collection - */ - public final ConditionsRecordCollection sortedByKey() { - return sorted(new KeyComparator()); - } - - /** - * Sort the collection in place. - * - * @param comparator the comparison to use for sorting - */ - public final void sort(final Comparator<ConditionsRecord> comparator) { - final List<ConditionsRecord> list = new ArrayList<ConditionsRecord>(this); - Collections.sort(list, comparator); - this.clear(); - this.addAll(list); - } - - /** - * Sort in place by updated date. - */ - public final void sortByUpdated() { - this.sort(new UpdatedComparator()); - } - - /** - * Sort in place by creation date. - */ - public final void sortByCreated() { - sort(new CreatedComparator()); - } - - /** - * Sort in place by run start. - */ - public final void sortByRunStart() { - sort(new RunStartComparator()); - } - - /** - * Sort in place by key. - */ - public final void sortByKey() { - sort(new KeyComparator()); - } - - /** - * Get the unique conditions keys from the records in this collection. - * - * @return the set of unique conditions keys - */ - public final Set<String> getConditionsKeys() { - final Set<String> conditionsKeys = new HashSet<String>(); - for (ConditionsRecord record : this) { - conditionsKeys.add(record.getName()); + * Compare conditions records by creation date. + */ + private static class CreatedComparator implements Comparator<ConditionsRecord> { + /** + * Compare the creation dates of two conditions records. + * + * @param c1 The first conditions record. + * @param c2 The second conditions record. + * @return -1, 0, or 1 if first date is less than, equal to, or greater than the second date. + */ + @Override + public int compare(final ConditionsRecord c1, final ConditionsRecord c2) { + final Date date1 = c1.getCreated(); + final Date date2 = c2.getCreated(); + if (date1.before(date2)) { + return -1; + } else if (date1.after(date2)) { + return 1; + } + return 0; } - return conditionsKeys; + } + + /** + * Compare conditions records by their key (table name). + */ + private static class KeyComparator implements Comparator<ConditionsRecord> { + /** + * Compare the keys (names) of two conditions records. + * + * @param c1 The first conditions record. + * @param c2 The second conditions record. + * @return -1, 0, or 1 if first name is less than, equal to, or greater than the second (using alphabetic + * comparison). + */ + @Override + public int compare(final ConditionsRecord c1, final ConditionsRecord c2) { + return c1.getName().compareTo(c2.getName()); + } } /** @@ -141,6 +80,7 @@ private static class RunStartComparator implements Comparator<ConditionsRecord> { /** * Compare the run start numbers of two conditions records. + * * @param c1 The first conditions record. * @param c2 The second conditions record. * @return -1, 0, or 1 if first run number is less than, equal to, or greater than the second. @@ -162,6 +102,7 @@ private static class UpdatedComparator implements Comparator<ConditionsRecord> { /** * Compare the updated dates of two conditions records. + * * @param c1 The first conditions record. * @param c2 The second conditions record. * @return -1, 0, or 1 if first date is less than, equal to, or greater than the second date. @@ -180,44 +121,132 @@ } /** - * Compare conditions records by creation date. - */ - private static class CreatedComparator implements Comparator<ConditionsRecord> { - /** - * Compare the creation dates of two conditions records. - * @param c1 The first conditions record. - * @param c2 The second conditions record. - * @return -1, 0, or 1 if first date is less than, equal to, or greater than the second date. - */ - @Override - public int compare(final ConditionsRecord c1, final ConditionsRecord c2) { - final Date date1 = c1.getCreated(); - final Date date2 = c2.getCreated(); - if (date1.before(date2)) { - return -1; - } else if (date1.after(date2)) { - return 1; - } - return 0; + * Get the unique conditions keys from the records in this collection. + * + * @return the set of unique conditions keys + */ + public final Set<String> getConditionsKeys() { + final Set<String> conditionsKeys = new HashSet<String>(); + for (final ConditionsRecord record : this) { + conditionsKeys.add(record.getName()); } - } - - /** - * Compare conditions records by their key (table name). - */ - private static class KeyComparator implements Comparator<ConditionsRecord> { - /** - * Compare the keys (names) of two conditions records. - * @param c1 The first conditions record. - * @param c2 The second conditions record. - * @return -1, 0, or 1 if first name is less than, equal to, or greater than the second - * (using alphabetic comparison). - */ - @Override - public int compare(final ConditionsRecord c1, final ConditionsRecord c2) { - return c1.getName().compareTo(c2.getName()); - } - } + return conditionsKeys; + } + + /** + * Sort the collection in place. + * + * @param comparator the comparison to use for sorting + */ + @Override + public final void sort(final Comparator<ConditionsRecord> comparator) { + final List<ConditionsRecord> list = new ArrayList<ConditionsRecord>(this); + Collections.sort(list, comparator); + this.clear(); + this.addAll(list); + } + + /** + * Sort in place by creation date. + */ + public final void sortByCreated() { + sort(new CreatedComparator()); + } + + /** + * Sort in place by key. + */ + public final void sortByKey() { + sort(new KeyComparator()); + } + + /** + * Sort in place by run start. + */ + public final void sortByRunStart() { + sort(new RunStartComparator()); + } + + /** + * Sort in place by updated date. + */ + public final void sortByUpdated() { + this.sort(new UpdatedComparator()); + } + + /** + * Sort using a comparator and leave the original collection unchanged. + * + * @param comparator the comparison to use for sorting + * @return the sorted collection + */ + @Override + public final ConditionsRecordCollection sorted(final Comparator<ConditionsRecord> comparator) { + final List<ConditionsRecord> list = new ArrayList<ConditionsRecord>(this); + Collections.sort(list, comparator); + final ConditionsRecordCollection collection = new ConditionsRecordCollection(); + collection.addAll(list); + return collection; + } + + /** + * Sort and return collection by creation date. + * + * @return the sorted collection + */ + public final ConditionsRecordCollection sortedByCreated() { + return sorted(new CreatedComparator()); + } + + /** + * Sort and return by key (table name). + * + * @return the sorted collection + */ + public final ConditionsRecordCollection sortedByKey() { + return sorted(new KeyComparator()); + } + + /** + * Sort and return by run start number. + * + * @return the sorted collection + */ + public final ConditionsRecordCollection sortedByRunStart() { + return sorted(new RunStartComparator()); + } + + /** + * Sort and return collection by updated date. + * + * @return the sorted collection + */ + public final ConditionsRecordCollection sortedByUpdated() { + return sorted(new UpdatedComparator()); + } + } + + /** + * Create a "blank" conditions record. + */ + public ConditionsRecord() { + } + + /** + * Copy constructor. + * + * @param record the <code>ConditionsRecord</code> to copy + */ + public ConditionsRecord(final ConditionsRecord record) { + this.setFieldValue("collection_id", record.getCollectionId()); + this.setFieldValue("run_start", record.getRunStart()); + this.setFieldValue("run_end", record.getRunEnd()); + this.setFieldValue("name", record.getName()); + this.setFieldValue("table_name", record.getTableName()); + this.setFieldValue("notes", record.getNotes()); + this.setFieldValue("tag", record.getTag()); + this.setFieldValue("created", record.getCreated()); + this.setFieldValue("created_by", record.getCreatedBy()); } /** @@ -245,13 +274,109 @@ } /** - * Create a "blank" conditions record. - */ - public ConditionsRecord() { + * Get the collection ID, overriding this method from the parent class. + * + * @return the collection ID + */ + @Field(names = {"collection_id"}) + public int getCollectionId() { + return getFieldValue("collection_id"); + } + + /** + * Get the date this record was created. + * + * @return the date this record was created + */ + @Field(names = {"created"}) + public Date getCreated() { + return getFieldValue("created"); + } + + /** + * Get the name of the user who created this record. + * + * @return the name of the person who created the record + */ + @Field(names = {"created_by"}) + public String getCreatedBy() { + return getFieldValue("created_by"); + } + + /** + * Get the name of these conditions. This is called the "key" in the table meta data to distinguish it from + * "table name" but it is usually the same value. + * + * @return the name of the conditions + */ + @Field(names = {"name"}) + public String getName() { + return getFieldValue("name"); + } + + /** + * Get the notes. + * + * @return the notes about this condition + */ + @Field(names = {"notes"}) + public String getNotes() { + return getFieldValue("notes"); + } + + /** + * Get the ending run number. + * + * @return the ending run number + */ + @Field(names = {"run_end"}) + public int getRunEnd() { + return getFieldValue("run_end"); + } + + /** + * Get the starting run number. + * + * @return the starting run number + */ + @Field(names = {"run_start"}) + public int getRunStart() { + return getFieldValue("run_start"); + } + + /** + * Get the name of the table containing the actual raw conditions data. + * + * @return the name of the table with the conditions data + */ + @Field(names = {"table_name"}) + public String getTableName() { + return getFieldValue("table_name"); + } + + /** + * Get the string tag associated with these conditions. + * + * @return The string tag. + */ + @Field(names = {"tag"}) + public String getTag() { + return getFieldValue("tag"); + } + + /** + * Get the date this record was last updated. + * + * @return the date this record was updated + */ + @Field(names = {"updated"}) + public Date getUpdated() { + return getFieldValue("updated"); } /** * Insert the conditions record into the database. + * * @throws ConditionsObjectException if there are errors inserting the record */ public void insert() throws ConditionsObjectException { @@ -273,111 +398,11 @@ } /** - * Get the starting run number. - * - * @return the starting run number - */ - @Field(names = { "run_start" }) - public int getRunStart() { - return getFieldValue("run_start"); - } - - /** - * Get the ending run number. - * - * @return the ending run number - */ - @Field(names = { "run_end" }) - public int getRunEnd() { - return getFieldValue("run_end"); - } - - /** - * Get the date this record was last updated. - * - * @return the date this record was updated - */ - @Field(names = { "updated" }) - public Date getUpdated() { - return getFieldValue("updated"); - } - - /** - * Get the date this record was created. - * - * @return the date this record was created - */ - @Field(names = { "created" }) - public Date getCreated() { - return getFieldValue("created"); - } - - /** - * Get the name of the user who created this record. - * - * @return the name of the person who created the record - */ - @Field(names = { "created_by" }) - public String getCreatedBy() { - return getFieldValue("created_by"); - } - - /** - * Get the notes. - * - * @return the notes about this condition - */ - @Field(names = { "notes" }) - public String getNotes() { - return getFieldValue("notes"); - } - - /** - * Get the name of these conditions. This is called the "key" in the table - * meta data to distinguish it from "table name" but it is usually the same value. - * - * @return the name of the conditions - */ - @Field(names = { "name" }) - public String getName() { - return getFieldValue("name"); - } - - /** - * Get the name of the table containing the actual raw conditions data. - * - * @return the name of the table with the conditions data - */ - @Field(names = { "table_name" }) - public String getTableName() { - return getFieldValue("table_name"); - } - - /** - * Get the collection ID, overriding this method from the parent class. - * - * @return the collection ID - */ - @Field(names = { "collection_id" }) - public int getCollectionId() { - return getFieldValue("collection_id"); - } - - /** - * Get the string tag associated with these conditions. - * - * @return The string tag. - */ - @Field(names = { "tag" }) - public String getTag() { - return getFieldValue("tag"); - } - - /** * Convert this record to a human readable string, one field per line. * * @return this object represented as a string */ + @Override public String toString() { final StringBuffer sb = new StringBuffer(); sb.append("id: " + getRowId() + '\n');