- added test code (in comments)
authorPatric Plitzner <p.plitzner@bgbm.org>
Wed, 30 Oct 2013 14:24:36 +0000 (14:24 +0000)
committerPatric Plitzner <p.plitzner@bgbm.org>
Wed, 30 Oct 2013 14:24:36 +0000 (14:24 +0000)
eu.etaxonomy.taxeditor.store/src/main/java/eu/etaxonomy/taxeditor/model/AbstractUtility.java

index b40d349320f1c713889dd6af26036c69c3cadcd9..ec74879c81d92101dabab082231e0fb74409ac49 100644 (file)
@@ -924,6 +924,7 @@ public abstract class AbstractUtility {
                }
        }
 
                }
        }
 
+
     /**
      * Orders a Collection of {@link IEnumTerm}s according to the term
      * hierarchy. The hierarchy will be reduced to two layers: one layer being
     /**
      * Orders a Collection of {@link IEnumTerm}s according to the term
      * hierarchy. The hierarchy will be reduced to two layers: one layer being
@@ -1007,4 +1008,126 @@ public abstract class AbstractUtility {
         }
     }
 
         }
     }
 
+    
+//    /**
+//     * Orders a Collection of {@link IEnumTerm}s according to the term
+//     * hierarchy. The hierarchy will be reduced to two layers: one layer being
+//     * the root elements (that have no parents) and the other being their
+//     * children and children's children recursively.<br>
+//     * The returned map will be be ordered primarily by root elements and
+//     * secondarily by the child elements, both ascending alphabetically. <br>
+//     * <br>
+//     * The reduced hierarchy could look like this:<br>
+//     * <ul>
+//     * <li>Root1
+//     *  <ul>
+//     *   <li>child1
+//     *   <li>child2
+//     *   <li>childOfChild2
+//     *  </ul>
+//     * <li>root2
+//     * <ul><li>child4</ul>
+//     * </ul>
+//     *
+//     * @param terms
+//     *            A {@link Collection} of {@link IEnumTerm}s for which the term
+//     *            hierarchy should be created
+//     * @return a map which holds the terms as keys and their string
+//     *         representation via {@link IEnumTerm#getMessage()} as values
+//     */
+//    public static <T extends IEnumTerm<?>> LinkedHashMap<T, String> orderTerms(Collection<T> terms) {
+//        Comparator<TermNode<T>> comparator = new Comparator<TermNode<T>>() {
+//            @Override
+//            public int compare(TermNode<T> t1, TermNode<T> t2) {
+//                return t1.getTerm().getMessage().compareTo(t2.getTerm().getMessage());
+//            }
+//        };
+//        TreeSet<TermNode<T>> parentElements = new TreeSet<TermNode<T>>(comparator);
+//        for(T term : terms) {
+//            Set<T> childList = new TreeSet<T>(comparator);
+//            // add root element as keys
+//            if(term.getKindOf()==null){
+//                parentElements.add(new TermNode<T>(term));
+//            }
+//            // add child element to parent
+//            else{
+//                T parent = getParentFor(term);
+//                if(termHierarchy.containsKey(parent)){
+//                    termHierarchy.get(parent).add(term);
+//                }
+//                else{
+//                    childList.add(term);
+//                    termHierarchy.put(parent, childList);
+//                }
+//            }
+//        }
+//
+//        // create list according to the type hierarchy (root elements alphabetically with recursive children also alphabetically)
+//        LinkedHashMap<T, String> result = new LinkedHashMap<T, String>();
+//        for(Entry<T, Set<T>> entry:termHierarchy.entrySet()){
+//            T root = entry.getKey();
+//            result.put(root, root.getMessage());
+//            for(T child:entry.getValue()){
+//                result.put(child, "  " + child.getMessage());
+//            }
+//        }
+//        return result;
+//    }
+//
+//    private static addToParents(TreeSet<TermNode<T>> parents, Collection<T> terms){
+//        for(T term:terms){
+//
+//        }
+//    }
+//
+//    @SuppressWarnings("unchecked")
+//    /**
+//     * Recursively iterates over all term parents until no more parent is found i.e. the root node
+//     * @param term The term for which the parent should be found
+//     * @return the root terms of the term hierarchy
+//     */
+//    private static<T extends IEnumTerm<?>> T getParentFor(T term){
+//        // PP: cast should be safe. Why is Eclipse complaining??
+//        T parent = (T) term.getKindOf();
+//        if(parent==null){
+//            return term;
+//        }
+//        else{
+//            return getParentFor((T) term.getKindOf());
+//        }
+//    }
+//
+//    private class TermNode<T>{
+//        private final T term;
+//        private final TreeSet<TermNode<T>> children;
+//
+//        /**
+//         * @param term
+//         * @param children
+//         */
+//        public TermNode(T term) {
+//            super();
+//            this.term = term;
+//            this.children = new TreeSet<TermNode<T>>();
+//        }
+//
+//        public void addChild(TermNode<T> child){
+//            this.children.add(child);
+//        }
+//
+//        /**
+//         * @return the children
+//         */
+//        public TreeSet<TermNode<T>> getChildren() {
+//            return children;
+//        }
+//
+//        /**
+//         * @return the term
+//         */
+//        public T getTerm() {
+//            return term;
+//        }
+//    }
+
 }
 }