Package net.sourceforge.chaperon.model.extended

Source Code of net.sourceforge.chaperon.model.extended.CharacterInterval

/*
*  Copyright (C) Chaperon. All rights reserved.
*  -------------------------------------------------------------------------
*  This software is published under the terms of the Apache Software License
*  version 1.1, a copy of which has been included  with this distribution in
*  the LICENSE file.
*/

package net.sourceforge.chaperon.model.extended;

import net.sourceforge.chaperon.common.Decoder;
import net.sourceforge.chaperon.model.Violations;

/**
* This class represents a interval of characters within a character class.
*
* @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
* @version CVS $Id: CharacterInterval.java,v 1.1 2003/12/12 14:11:34 benedikta Exp $
*/
public class CharacterInterval
{
  private SingleCharacter first = null;
  private SingleCharacter last = null;
  private String location = null;

  /**
   * Creates a interval of characters.
   */
  public CharacterInterval() {}

  /**
   * Sets the first character for the interval.
   *
   * @param first First character.
   */
  public void setFirstCharacter(SingleCharacter first)
  {
    if ((this.last==null) || (this.last.getCharacter()<first.getCharacter()))
      this.last = first;

    this.first = first;
  }

  /**
   * Returns the first character for the interval.
   *
   * @return First character.
   */
  public SingleCharacter getFirstCharacter()
  {
    return this.first;
  }

  /**
   * Sets the last character for the interval.
   *
   * @param last Last character.
   */
  public void setLastCharacter(SingleCharacter last)
  {
    if ((this.first==null) || (this.first.getCharacter()>last.getCharacter()))
      this.first = last;

    this.last = last;
  }

  /**
   * Returns the last character for the interval.
   *
   * @return Last character.
   */
  public SingleCharacter getLastCharacter()
  {
    return this.last;
  }

  public void addSingleCharacter(SingleCharacter character)
  {
    if (character==null)
      return;

    if (getLastCharacter()!=null)
      setFirstCharacter(getLastCharacter());

    setLastCharacter(character);
  }

  public SingleCharacter[] getSingleCharacters()
  {
    return new SingleCharacter[]{first, last};
  }

  public char[] getLimits()
  {
    return new char[]{first.getCharacter(), last.getCharacter()};
  }

  public boolean contains(char minimum, char maximum)
  {
    return (first.getCharacter()<=minimum) && (minimum<=last.getCharacter()) &&
           (first.getCharacter()<=maximum) && (maximum<=last.getCharacter());
  }

  public boolean contains(char c)
  {
    return (first.getCharacter()<=c) && (c<=last.getCharacter());
  }

  /**
   * Set the location from the input source.
   *
   * @param location Location in the input source.
   */
  public void setLocation(String location)
  {
    this.location = location;
  }

  /**
   * Returns the location from the input source.
   *
   * @return Location in the input source.
   */
  public String getLocation()
  {
    return location;
  }

  /**
   * Return a string representation of this pattern
   *
   * @return String representation of the pattern.
   */
  public String toString()
  {
    StringBuffer buffer = new StringBuffer();

    buffer.append(Decoder.decode(this.first.getCharacter(), Decoder.CLASS));
    buffer.append("-");
    buffer.append(Decoder.decode(this.last.getCharacter(), Decoder.CLASS));
    return buffer.toString();
  }

  /**
   * Create a clone of this pattern.
   *
   * @return Clone of this pattern.
   *
   * @throws CloneNotSupportedException If an exception occurs during the cloning.
   */
  public Object clone()
  {
    CharacterInterval clone = new CharacterInterval();

    clone.setFirstCharacter(getFirstCharacter());
    clone.setLastCharacter(getLastCharacter());

    return clone;
  }

  /**
   * Validates this pattern.
   *
   * @return Return a list of violations, if this pattern isn't valid.
   */
  public Violations validate()
  {
    Violations violations = new Violations();

    if ((first==null) || (last==null))
      violations.addViolation("Interval is incomplete", getLocation());

    if (first.getCharacter()>last.getCharacter())
      violations.addViolation("First is greater than the last", getLocation());

    if (first.getCharacter()==last.getCharacter())
      violations.addViolation("First is equal than the last", getLocation());

    return violations;
  }
}
TOP

Related Classes of net.sourceforge.chaperon.model.extended.CharacterInterval

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.