Comb.java :  » MVC » honeycomb » net » lucubrators » honeycomb » core » Java Open Source

Java Open Source » MVC » honeycomb 
honeycomb » net » lucubrators » honeycomb » core » Comb.java
/**
 * Copyright (c) 2010 Alois Glomann gnalff@lucubrators.net
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package net.lucubrators.honeycomb.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.lucubrators.honeycomb.core.constraint.ConstraintHolder;

/**
 * The class, this engine is all about. A comb is actually nothing but a
 * container for the controller, the attributes and applied constraints. A comb
 * also represents the structure of a page.
 * 
 * @author gnalFF
 * 
 */
public final class Comb {

  private String          id;
  private Object          controller;
  private HashMap<String, String>  attributes;
  private Map<String, List<Comb>>  marks;
  private String          eventName;
  private List<ConstraintHolder>  constraints;

  /**
   * Creates a new comb with id and controller
   * 
   * @param id
   *            the id of the comb to be created.
   * @param controller
   *            the controller to be added to the comb
   * @throws NullPointerException
   *             if id or controller is null.
   */
  public Comb(final String id, final Object controller) {
    if (id == null || controller == null) {
      throw new NullPointerException(
          "Id or controller must not be null. Id was: >" + id
              + "< Controller was: >" + controller + "<");
    }
    this.id = id;
    this.controller = controller;
    this.attributes = new HashMap<String, String>();
    this.marks = new HashMap<String, List<Comb>>();
    this.constraints = new ArrayList<ConstraintHolder>();
  }

  /**
   * @return the id of the comb
   */
  public String getId() {
    return id;
  }

  /**
   * @return the controller of the comb
   */
  public Object getController() {
    return controller;
  }

  /**
   * Adds marks to the comb. a mark is actually a map containing nested combs
   * 
   * @param marks
   *            a map containing sub combs
   */
  public void setMarks(final Map<String, List<Comb>> marks) {
    this.marks.putAll(marks);
  }

  /**
   * 
   * @return sub combs applied to the comb, never null
   */
  public Map<String, List<Comb>> getMarks() {
    return marks;
  }

  /**
   * adds attributes to the comb
   * 
   * @param attributes
   *            a map of attributes
   */
  public void setAttributes(final Map<String, String> attributes) {
    this.attributes.putAll(attributes);
  }

  /**
   * 
   * @return a map of attributes, never null. The map is not backed by the
   *         comb, so adding and removing items has no effect.
   */
  public Map<String, String> getAttributes() {
    return new HashMap<String, String>(attributes);
  }

  /**
   * Adds constraints to the comb
   * 
   * @param constraintHolders
   *            a list of all constraints to be applied
   */
  public void setConstraintHolders(final List<ConstraintHolder> constraintHolders) {
    this.constraints.addAll(constraintHolders);
  }

  /**
   * 
   * @return a list of constraints applied to the comb. never null
   */
  public List<ConstraintHolder> getConstraintHolders() {
    return constraints;
  }

  public void setEventName(final String eventName) {
    this.eventName = eventName;
  }

  /**
   * 
   * @return the name of the event the comb listens on.
   */
  public String getEventName() {
    return eventName;
  }

  /**
   * @return a copy of this comb. It will be a new instance of this comb.
   *         Submarks will be just copied by reference into the new map as
   *         well as constraints.
   */
  public Comb copy() {
    return alias(this.getId());
  }

  /**
   * The same as {@link Comb#copy()} except that you can change the id of the
   * newly created comb
   * 
   * @param alias
   *            the id which the new comb should have
   * @return a new instance of this comb
   */
  public Comb alias(final String alias) {
    Comb copy = new Comb(alias, getController());
    copy.setAttributes(getAttributes());
    copy.setMarks(getMarks());
    copy.setEventName(getEventName());
    copy.setConstraintHolders(getConstraintHolders());
    return copy;
  }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.