All Downloads are FREE. Search and download functionalities are using the official Maven repository.

ro.isdc.wro.model.WroModelInspector Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package ro.isdc.wro.model;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;

import ro.isdc.wro.model.group.Group;
import ro.isdc.wro.model.resource.Resource;


/**
 * Helper for querying a snapshot of a model. Any changes of the model performed after model inspector instantiation
 * will not be reflected.
 *
 * @author Alex Objelean
 * @created 21 Sep 2012
 * @since 1.5.0
 */
public class WroModelInspector {
  private final Map map = new HashMap();
  public WroModelInspector(final WroModel model) {
    Validate.notNull(model);
    for (final Group group : model.getGroups()) {
      map.put(group.getName(), group);
    }
  }

  /**
   * @param name
   *          of group to find.
   * @return group with searched name (if available) or null otherwise.
   *
   */
  public Group getGroupByName(final String name) {
    return map.get(name);
  }

  /**
   * @param resource
   *          the {@link Resource} to search in all available groups.
   * @return t collection of group names containing provided resource. If the resource is not availalbe, an empty collection
   *         will be returned.
   */
  public Collection getGroupNamesContainingResource(final String resourceUri) {
    Validate.notNull(resourceUri);
    final Set groupNames = new TreeSet();
    for (final Group group : map.values()) {
      if (group.hasResource(resourceUri)) {
        groupNames.add(group.getName());
      }
    }
    return groupNames;
  }

  /**
   * @return a set of group names.
   */
  public final List getGroupNames() {
    //decorate with tree set to keep them sorted (for jdk5).
    return new ArrayList(new TreeSet(map.keySet()));
  }

  /**
   * @param groupName the nam of the group to check.
   * @return true if the provided groupName is available.
   */
  public boolean hasGroup(final String groupName) {
    return map.containsKey(groupName);
  }

  /**
   * This implementation would be simpler if java would have closures :).
   *
   * @return a comma separated list of group names.
   */
  public String getGroupNamesAsString() {
    return String.format("%s", StringUtils.join(getGroupNames(), ", "));
  }

  /**
   * @return the set of all unique resources from all the groups of the model (no particular order).
   */
  public Collection getAllUniqueResources() {
    return collectResources(new HashSet());
  }

  /**
   * @return the set of all resources from all the groups of the model (no particular order).
   */
  public Collection getAllResources() {
    return collectResources(new ArrayList());
  }

  private Collection collectResources(final Collection resources) {
    for (final Group group : map.values()) {
      resources.addAll(group.getResources());
    }
    return resources;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy