Print

Print


Author: [log in to unmask]
Date: Mon Apr 27 16:01:14 2015
New Revision: 2833

Log:
Add method to find conditions records that match a certain key.  Override add method so that undesirable behavior in super class does not execute.

Modified:
    java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/ConditionsRecord.java

Modified: java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/ConditionsRecord.java
 =============================================================================
--- java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/ConditionsRecord.java	(original)
+++ java/branches/HPSJAVA-488/conditions/src/main/java/org/hps/conditions/api/ConditionsRecord.java	Mon Apr 27 16:01:14 2015
@@ -26,6 +26,18 @@
     @SuppressWarnings("serial")
     public static class ConditionsRecordCollection extends BaseConditionsObjectCollection<ConditionsRecord> {
 
+        @Override
+        public boolean add(final ConditionsRecord object) throws ConditionsObjectException {
+            if (object == null) {
+                throw new IllegalArgumentException("The object argument is null.");
+            }
+            final boolean added = getObjects().add(object);
+            if (!added) {
+                throw new RuntimeException("Failed to add object.");
+            }
+            return added;
+        }
+    	
         /**
          * Compare conditions records by creation date.
          */
@@ -190,6 +202,23 @@
         public final ConditionsRecordCollection sortedByUpdated() {
             return (ConditionsRecordCollection) sorted(new UpdatedComparator());
         }
+        
+        /**
+         * Find a sub-set of the records with matching key name.
+         */
+        public ConditionsRecordCollection findByKey(String key) {
+        	ConditionsRecordCollection collection = new ConditionsRecordCollection();
+        	for (ConditionsRecord record : this) {
+        		if (record.getName().equals(key)) {
+        			try {
+						collection.add(record);
+					} catch (ConditionsObjectException e) {
+						throw new RuntimeException("Error adding record to new collection.", e);
+					}
+        		}
+        	}
+        	return collection;
+        }
     }
 
     /**