/*
* 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.pattern;
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.5 2003/12/09 19:55:52 benedikta Exp $
*/
public class CharacterInterval implements CharacterClassElement
{
private char minimum = 'a';
private char maximum = 'z';
private String location = null;
/**
* Creates a interval of characters.
*/
public CharacterInterval() {}
/**
* Sets the minimum character for the interval.
*
* @param minimum Minimum character.
*/
public void setMinimum(char minimum)
{
this.minimum = minimum;
}
/**
* Returns the minimum character for the interval.
*
* @return Minimum character.
*/
public char getMinimum()
{
return this.minimum;
}
/**
* Sets the maximum character for the interval.
*
* @param maximum Maximum character.
*/
public void setMaximum(char maximum)
{
this.maximum = maximum;
}
/**
* Returns the maximum character for the interval.
*
* @return Maximum character.
*/
public char getMaximum()
{
return this.maximum;
}
/**
* 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.minimum, Decoder.CLASS));
buffer.append("-");
buffer.append(Decoder.decode(this.maximum, 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.setMinimum(getMinimum());
clone.setMaximum(getMaximum());
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 (minimum>maximum)
violations.addViolation("Minimum is greater than the maximum", getLocation());
if (minimum==maximum)
violations.addViolation("Minimum is equal than the maximum", getLocation());
return violations;
}
}