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

com.jidesoft.range.Category Maven / Gradle / Ivy

/*
 * @(#)Category.java
 * 
 * 2002 - 2009 JIDE Software Incorporated. All rights reserved.
 * Copyright (c) 2005 - 2009 Catalysoft Limited. All rights reserved.
 */

package com.jidesoft.range;

/**
 * This class is really an adapter because it takes any object and allows it to be used as a Category.
 *
 * @author Simon White ([email protected])
 */
public class Category implements Positionable {
    private String _name;
    private T _value;
    private CategoryRange _range;

    public Category(String name, T value) {
        setName(name);
        _value = value;
    }

    public Category(String name, T value, CategoryRange range) {
        setName(name);
        _value = value;
        _range = range;
    }

    public Category(T value) {
        _value = value;
    }

    public Category(T value, CategoryRange range) {
        _value = value;
        _range = range;
    }

    public CategoryRange getRange() {
        return _range;
    }

    public void setRange(CategoryRange range) {
        _range = range;
    }

    public double position() {
        if (_range == null) {
            throw new IllegalStateException("Cannot compute position for a category that does not belong to a range");
        }
        return _range.position(_value);
    }

    public T getValue() {
        return _value;
    }

    public String getName() {
        if (_name == null) {
            return _value.toString();
        }
        return _name;
    }

    private void setName(String name) {
        _name = name;
    }

    public int compareTo(Positionable o) {
        double otherPosition = o.position();
        double position = position();
        if (position < otherPosition) {
            return -1;
        }
        else if (position > otherPosition) {
            return 1;
        }
        else {
            return 0;
        }
    }
    
    @Override
    public boolean equals(Object other) {
        return super.equals(other);
    }

    @Override
    public String toString() {
        return String.format("#", _name, _value.toString());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy