Commits

Anonymous committed 9387d4d Merge

merge AbstractIndexedLabelFunction fixes from 1.5.4

Comments (0)

Files changed (4)

 15a3eb13fd730a88eae33c86e664af2456fb6cf6 v1.5.1
 f7fcdfc132c24b9f3c371d73430de818dc146dca v1.5.2
 a395011cf148625a5a7f55455c37d823932c5222 v1.5.3
+2ae7814db35b02c80a1ae07dc777ef1f81fadc2c v1.5.4
 04e8e623ae12025df0d606e2cc2ea9e82ca99196 v2.0.0
   <groupId>org.nrg</groupId>
   <artifactId>DicomEdit</artifactId>
   <packaging>jar</packaging>
-  <version>2.0.0</version>
+  <version>2.0.1</version>
   <name>DicomEdit</name>
   <description>Implementation of a language for modifying DICOM metadata</description>
   <url>http://nrg.wustl.edu</url>

src/main/java/org/nrg/dcm/edit/AbstractIndexedLabelFunction.java

 /**
- * Copyright (c) 2010,2011 Washington University
+ * Copyright (c) 2010-2012 Washington University
  */
 package org.nrg.dcm.edit;
 
  *
  */
 public abstract class AbstractIndexedLabelFunction implements ScriptFunction {
-	protected abstract boolean isAvailable(String label) throws ScriptEvaluationException;
-	
-	private final Value getFormat(final List<Value> values) throws ScriptEvaluationException {
-		try {
-			return values.get(0);
-		} catch (IndexOutOfBoundsException e) {
-			try {
-				throw new ScriptEvaluationException(this.getClass().getField("name").get(null)
-						+ " requires format argument");
-			} catch (IllegalArgumentException e1) {
-				throw new RuntimeException(e1);
-			} catch (SecurityException e1) {
-				throw new RuntimeException(e1);
-			} catch (IllegalAccessException e1) {
-				throw new RuntimeException(e1);
-			} catch (NoSuchFieldException e1) {
-				throw new RuntimeException(e1);
-			}
+    protected abstract boolean isAvailable(String label) throws ScriptEvaluationException;
+    
+    private final Value getFormat(final List<Value> values)
+    throws ScriptEvaluationException {
+	try {
+	    return values.get(0);
+	} catch (IndexOutOfBoundsException e) {
+	    try {
+		throw new ScriptEvaluationException(this.getClass().getField("name").get(null)
+						    + " requires format argument");
+	    } catch (IllegalArgumentException e1) {
+		throw new RuntimeException(e1);
+	    } catch (SecurityException e1) {
+		throw new RuntimeException(e1);
+	    } catch (IllegalAccessException e1) {
+		throw new RuntimeException(e1);
+	    } catch (NoSuchFieldException e1) {
+		throw new RuntimeException(e1);
+	    }
+	}
+    }
+    
+    /* (non-Javadoc)
+     * @see org.nrg.dcm.edit.ScriptFunction#apply(java.util.List)
+     */
+    public Value apply(final List<Value> args) throws ScriptEvaluationException {
+	final Value format = getFormat(args);
+	return new Value() {
+	    private NumberFormat buildFormatter(final int len) {
+		final StringBuilder sb = new StringBuilder();
+		for (int i = 0; i < len; i++) {
+		    sb.append("0");
 		}
-	}
-	
-	/* (non-Javadoc)
-	 * @see org.nrg.dcm.edit.ScriptFunction#apply(java.util.List)
-	 */
-	public Value apply(final List<Value> args) throws ScriptEvaluationException {
-		final Value format = getFormat(args);
-		return new Value() {
-			private NumberFormat buildFormatter(final int len) {
-				final StringBuilder sb = new StringBuilder();
-				for (int i = 0; i < len; i++) {
-					sb.append("0");
-				}
-				return new DecimalFormat(sb.toString());
-			}
-
-			private String valueFor(final String format) throws ScriptEvaluationException {
-				final int offset = format.indexOf('#');
-				int end = offset;
-				while ('#' == format.charAt(end)) {
-					end++;
-				}
-				final NumberFormat nf = buildFormatter(end - offset);
-				
-				final StringBuffer sb = new StringBuffer(format);
-
-				for (int i = 0; true; i++) {
-					final String label = sb.replace(offset, end, nf.format(i)).toString();
-					if (isAvailable(label)) {
-						return label;
-					}
-				}				
-			}
-			
-			public String on(final Map<Integer,String> m) throws ScriptEvaluationException {
-				return valueFor(format.on(m));
-			}
-			
-			public String on(final DicomObject o) throws ScriptEvaluationException {
-				return valueFor(format.on(o));
-			}
-			
-			public Set<Variable> getVariables() { return format.getVariables(); }
-			
-			public SortedSet<Integer> getTags() { return format.getTags(); }
-		};
-	}
-
+		return new DecimalFormat(sb.toString());
+	    }
+	    
+	    private String valueFor(final String format) throws ScriptEvaluationException {
+		final int offset = format.indexOf('#');
+		if (-1 == offset) {
+		    return "";
+		}
+		int end = offset;
+		while (end < format.length() && '#' == format.charAt(end)) {
+		    end++;
+		}
+		final NumberFormat nf = buildFormatter(end - offset);
+		
+		final StringBuffer sb = new StringBuffer(format);
+		
+		for (int i = 0; true; i++) {
+		    final String label = sb.replace(offset, end, nf.format(i)).toString();
+		    if (isAvailable(label)) {
+			return label;
+		    }
+		}				
+	    }
+	    
+	    public String on(final Map<Integer,String> m) throws ScriptEvaluationException {
+		return valueFor(format.on(m));
+	    }
+	    
+	    public String on(final DicomObject o) throws ScriptEvaluationException {
+		return valueFor(format.on(o));
+	    }
+	    
+	    public Set<Variable> getVariables() { return format.getVariables(); }
+	    
+	    public SortedSet<Integer> getTags() { return format.getTags(); }
+	};
+    }
 }

src/test/java/org/nrg/dcm/edit/AbstractIndexedLabelFunctionTest.java

+package org.nrg.dcm.edit;
+
+import java.util.Arrays;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Set;
+
+import org.dcm4che2.data.DicomObject;
+
+import com.google.common.collect.Lists;
+
+import junit.framework.TestCase;
+
+/**
+ * @author Kevin A. Archie <karchie@wustl.edu>
+ *
+ */
+public class AbstractIndexedLabelFunctionTest extends TestCase {
+    final class TestLabelFunction extends AbstractIndexedLabelFunction {
+        private final Set<String> defined;
+        
+        public TestLabelFunction(final String...defined) {
+            this.defined = new HashSet<String>(Arrays.asList(defined));
+        }
+        
+        public boolean isAvailable(final String label) {
+            return !defined.contains(label);
+        }
+    }
+    
+    private List<Value> format(final String format) {
+        final List<Value> l = Lists.newArrayList();
+        l.add(new ConstantValue(format));
+        return l;
+    }
+    
+    private final DicomObject o = null;
+    
+    public void testApply() throws ScriptEvaluationException{
+        assertEquals("", new TestLabelFunction().apply(format("")).on(o));
+        assertEquals("0", new TestLabelFunction().apply(format("#")).on(o));
+        assertEquals("1", new TestLabelFunction("0").apply(format("#")).on(o));
+        assertEquals("00", new TestLabelFunction().apply(format("##")).on(o));
+        assertEquals("01", new TestLabelFunction("00").apply(format("##")).on(o));
+        assertEquals("foo002bar", new TestLabelFunction("foo000bar","foo001bar")
+            .apply(format("foo###bar")).on(o));
+    }
+}