001package org.javasimon.console; 002 003import java.io.IOException; 004import java.util.ArrayList; 005import java.util.Collections; 006import java.util.Comparator; 007import java.util.List; 008import java.util.Set; 009 010import org.javasimon.Manager; 011import org.javasimon.Simon; 012import org.javasimon.SimonPattern; 013 014/** 015 * Helper class to navigate through Simons. 016 * 017 * @author gquintana 018 */ 019public class SimonVisitors { 020 021 private SimonVisitors() { 022 } 023 024 /** 025 * Visit simons as a list. 026 * Calls {@link Manager#getSimons(org.javasimon.SimonFilter)} method 027 * then Simons are sorted by name and filtered by type 028 * finally the visitor is called on each of them. 029 * 030 * @param manager Simon manager 031 * @param pattern Pattern 032 * @param types set of Simon types 033 * @param visitor Visitor 034 * @throws IOException 035 */ 036 public static void visitList(Manager manager, String pattern, Set<SimonType> types, SimonVisitor visitor) throws IOException { 037 List<Simon> simons = new ArrayList<>(manager.getSimons(SimonPattern.create(pattern))); 038 Collections.sort(simons, new Comparator<Simon>() { 039 public int compare(Simon s1, Simon s2) { 040 return s1.getName().compareTo(s2.getName()); 041 } 042 }); 043 for (Simon simon : simons) { 044 SimonType lType = SimonTypeFactory.getValueFromInstance(simon); 045 if (types == null || types.contains(lType)) { 046 visitor.visit(simon); 047 } 048 } 049 } 050 051 /** 052 * Visit Simons recursively as a tree starting from the specified Simon. 053 * 054 * @param simon Parent simon 055 * @param visitor Visitor 056 * @throws IOException 057 */ 058 public static void visitTree(Simon simon, SimonVisitor visitor) throws IOException { 059 visitor.visit(simon); 060 for (Simon childSimon : simon.getChildren()) { 061 visitTree(childSimon, visitor); 062 } 063 } 064}