]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - bundles/org.simantics.diagram/src/org/simantics/diagram/flag/PermutativeFlagLabelingScheme.java
Fixed all line endings of the repository
[simantics/platform.git] / bundles / org.simantics.diagram / src / org / simantics / diagram / flag / PermutativeFlagLabelingScheme.java
index de3782409bfa385a6c0ea66a638d2f6683b94b1b..ef8b09d28820a0e0776bd0e8d0876b6aedf3b8c9 100644 (file)
-/*******************************************************************************\r
- * Copyright (c) 2007, 2011 Association for Decentralized Information Management\r
- * in Industry THTH ry.\r
- * All rights reserved. This program and the accompanying materials\r
- * are made available under the terms of the Eclipse Public License v1.0\r
- * which accompanies this distribution, and is available at\r
- * http://www.eclipse.org/legal/epl-v10.html\r
- *\r
- * Contributors:\r
- *     VTT Technical Research Centre of Finland - initial API and implementation\r
- *******************************************************************************/\r
-package org.simantics.diagram.flag;\r
-\r
-import java.nio.CharBuffer;\r
-import java.util.Arrays;\r
-import java.util.Set;\r
-import java.util.TreeSet;\r
-\r
-import org.simantics.databoard.Bindings;\r
-import org.simantics.db.ReadGraph;\r
-import org.simantics.db.Resource;\r
-import org.simantics.db.common.primitiverequest.OrderedSet;\r
-import org.simantics.db.common.utils.NameUtils;\r
-import org.simantics.db.exception.DatabaseException;\r
-import org.simantics.diagram.stubs.DiagramResource;\r
-import org.simantics.layer0.Layer0;\r
-\r
-/**\r
- * @author Tuukka Lehtonen\r
- */\r
-public class PermutativeFlagLabelingScheme implements FlagLabelingScheme {\r
-\r
-    protected String initialValue;\r
-    protected char   min;\r
-    protected char   max;\r
-\r
-    /**\r
-     * @param initialValue\r
-     * @param min\r
-     * @param max\r
-     */\r
-    public PermutativeFlagLabelingScheme(String initialValue, char min, char max) {\r
-        this.initialValue = initialValue;\r
-        this.min = min;\r
-        this.max = max;\r
-    }\r
-\r
-    /**\r
-     * Support for graph adapter construction. Only uses the first character of\r
-     * both min and max parameters.\r
-     * \r
-     * @param initialValue\r
-     * @param min\r
-     * @param max\r
-     */\r
-    public PermutativeFlagLabelingScheme(String initialValue, String min, String max) {\r
-        this.initialValue = initialValue;\r
-        this.min = min.charAt(0);\r
-        this.max = max.charAt(0);\r
-    }\r
-\r
-    protected Set<String> getUsedLabels(ReadGraph graph, Resource diagram) throws DatabaseException {\r
-        DiagramResource DIA = DiagramResource.getInstance(graph);\r
-        Layer0 L0 = Layer0.getInstance(graph);\r
-        Set<String> used = new TreeSet<String>(NameUtils.STRING_CHARBUFFER_COMPARATOR);\r
-        for (Resource element : graph.syncRequest(new OrderedSet(diagram))) {\r
-            if (graph.isInstanceOf(element, DIA.Flag)) {\r
-                String label = graph.getPossibleRelatedValue(element, L0.HasLabel, Bindings.STRING);\r
-                if (label != null && !label.isEmpty())\r
-                    used.add(label);\r
-            }\r
-        }\r
-        return used;\r
-    }\r
-\r
-    @Override\r
-    public String generateLabel(ReadGraph graph, Resource diagram) throws DatabaseException {\r
-        CharBuffer cb = CharBuffer.allocate(10);\r
-        Set<String> used = getUsedLabels(graph, diagram);\r
-        initialValue(cb);\r
-        cb.limit(cb.position());\r
-        while (used.contains(cb)) {\r
-            permutate(cb);\r
-            cb.position(cb.limit());\r
-        }\r
-        cb.rewind();\r
-        return cb.toString();\r
-    }\r
-\r
-    protected void initialValue(CharBuffer cb) {\r
-        cb.append(initialValue);\r
-    }\r
-\r
-    protected void permutate(CharBuffer cb) {\r
-        permutate(cb, min, max);\r
-    }\r
-\r
-    /**\r
-     * Takes an input char buffer and modifies it so that its last character is\r
-     * set to the next value within the range [min,max]. If a character is\r
-     * already equal to max, this check and modify process is continued towards\r
-     * the beginning of the buffer until a character less than max is found. In\r
-     * the case where all characters are already equal to max, the buffer is set\r
-     * to size + 1 min characters, thus expanding the permutation space.\r
-     * \r
-     * For example permutating the string "AA" produces a sequence "AB", "AC",\r
-     * "AD", ..., "AZ", "BA", "BB", ...\r
-     */\r
-    protected void permutate(CharBuffer str, char min, char max) {\r
-        // Produce the next permutation of an [{min}-{max}]* character sequence.\r
-        int s = str.limit();\r
-        char[] chars = str.array();\r
-        for (int i = s - 1; i >= 0; --i) {\r
-            if (chars[i] < max) {\r
-                chars[i]++;\r
-                for (int j = i + 1; j < s; ++j)\r
-                    chars[j] = min;\r
-                return;\r
-            }\r
-        }\r
-        // all permutations already in use, add more characters.\r
-        str.limit(s + 1);\r
-        Arrays.fill(chars, 0, s + 1, min);\r
-    }\r
-\r
+/*******************************************************************************
+ * Copyright (c) 2007, 2011 Association for Decentralized Information Management
+ * in Industry THTH ry.
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License v1.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ *     VTT Technical Research Centre of Finland - initial API and implementation
+ *******************************************************************************/
+package org.simantics.diagram.flag;
+
+import java.nio.CharBuffer;
+import java.util.Arrays;
+import java.util.Set;
+import java.util.TreeSet;
+
+import org.simantics.databoard.Bindings;
+import org.simantics.db.ReadGraph;
+import org.simantics.db.Resource;
+import org.simantics.db.common.primitiverequest.OrderedSet;
+import org.simantics.db.common.utils.NameUtils;
+import org.simantics.db.exception.DatabaseException;
+import org.simantics.diagram.stubs.DiagramResource;
+import org.simantics.layer0.Layer0;
+
+/**
+ * @author Tuukka Lehtonen
+ */
+public class PermutativeFlagLabelingScheme implements FlagLabelingScheme {
+
+    protected String initialValue;
+    protected char   min;
+    protected char   max;
+
+    /**
+     * @param initialValue
+     * @param min
+     * @param max
+     */
+    public PermutativeFlagLabelingScheme(String initialValue, char min, char max) {
+        this.initialValue = initialValue;
+        this.min = min;
+        this.max = max;
+    }
+
+    /**
+     * Support for graph adapter construction. Only uses the first character of
+     * both min and max parameters.
+     * 
+     * @param initialValue
+     * @param min
+     * @param max
+     */
+    public PermutativeFlagLabelingScheme(String initialValue, String min, String max) {
+        this.initialValue = initialValue;
+        this.min = min.charAt(0);
+        this.max = max.charAt(0);
+    }
+
+    protected Set<String> getUsedLabels(ReadGraph graph, Resource diagram) throws DatabaseException {
+        DiagramResource DIA = DiagramResource.getInstance(graph);
+        Layer0 L0 = Layer0.getInstance(graph);
+        Set<String> used = new TreeSet<String>(NameUtils.STRING_CHARBUFFER_COMPARATOR);
+        for (Resource element : graph.syncRequest(new OrderedSet(diagram))) {
+            if (graph.isInstanceOf(element, DIA.Flag)) {
+                String label = graph.getPossibleRelatedValue(element, L0.HasLabel, Bindings.STRING);
+                if (label != null && !label.isEmpty())
+                    used.add(label);
+            }
+        }
+        return used;
+    }
+
+    @Override
+    public String generateLabel(ReadGraph graph, Resource diagram) throws DatabaseException {
+        CharBuffer cb = CharBuffer.allocate(10);
+        Set<String> used = getUsedLabels(graph, diagram);
+        initialValue(cb);
+        cb.limit(cb.position());
+        while (used.contains(cb)) {
+            permutate(cb);
+            cb.position(cb.limit());
+        }
+        cb.rewind();
+        return cb.toString();
+    }
+
+    protected void initialValue(CharBuffer cb) {
+        cb.append(initialValue);
+    }
+
+    protected void permutate(CharBuffer cb) {
+        permutate(cb, min, max);
+    }
+
+    /**
+     * Takes an input char buffer and modifies it so that its last character is
+     * set to the next value within the range [min,max]. If a character is
+     * already equal to max, this check and modify process is continued towards
+     * the beginning of the buffer until a character less than max is found. In
+     * the case where all characters are already equal to max, the buffer is set
+     * to size + 1 min characters, thus expanding the permutation space.
+     * 
+     * For example permutating the string "AA" produces a sequence "AB", "AC",
+     * "AD", ..., "AZ", "BA", "BB", ...
+     */
+    protected void permutate(CharBuffer str, char min, char max) {
+        // Produce the next permutation of an [{min}-{max}]* character sequence.
+        int s = str.limit();
+        char[] chars = str.array();
+        for (int i = s - 1; i >= 0; --i) {
+            if (chars[i] < max) {
+                chars[i]++;
+                for (int j = i + 1; j < s; ++j)
+                    chars[j] = min;
+                return;
+            }
+        }
+        // all permutations already in use, add more characters.
+        str.limit(s + 1);
+        Arrays.fill(chars, 0, s + 1, min);
+    }
+
 }
\ No newline at end of file