
org.decision_deck.jmcda.structure.Criterion Maven / Gradle / Ivy
Show all versions of base Show documentation
package org.decision_deck.jmcda.structure;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Objects.ToStringHelper;
/**
*
* A criterion object. Has a unique id (not null
).
*
*
* A criterion may also represent an attribute, depending on the context. An advantage is that a matrix of evaluation of
* alternatives against criteria may also be used to represent evaluations of alternatives against attributes. The
* difference exists when more context is known. Typically, a criterion has a preference direction, an attribute does
* not.
*
*
* Objects of this type are immutable.
*
*
* A criterion {@link #equals(Object)} an other one iff they have the same id.
*
*
* @author Olivier Cailloux
*
*/
public class Criterion implements Comparable {
/**
* Never null
.
*/
private final String m_id;
/**
* @param id
* not null
.
*/
public Criterion(final String id) {
checkNotNull(id);
m_id = id;
}
/**
* Creates a new criterion by copying the one given.
*
* @param criterion
* not null
.
*/
public Criterion(Criterion criterion) {
if (criterion == null) {
throw new NullPointerException();
}
m_id = criterion.m_id;
}
@Override
public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Criterion other = (Criterion) obj;
if (!m_id.equals(other.m_id)) {
return false;
}
return true;
}
public String getId() {
return m_id;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + (m_id.hashCode());
return result;
}
@Override
public String toString() {
final ToStringHelper helper = Objects.toStringHelper(this);
helper.addValue(m_id);
return helper.toString();
}
@Override
public int compareTo(Criterion o) {
return m_id.compareTo(o.m_id);
}
/**
*
* Retrieves a function which gives the id of the given criterion. No null
values are accepted.
*
*
* This provides an easy way to get short debug strings. E.g. to get a string representing the contents of a set of
* criteria s, use Joiner.on(", ").join(Iterables.transform(s, getIdFct()))
.
*
*
* @return not null
.
*/
static public Function getIdFct() {
final Function namer = new Function() {
@Override
public String apply(Criterion input) {
return input.getId();
}
};
return namer;
}
}