CategoricalValue.java

  1. package org.thegalactic.context.constraint.categorical;

  2. /*
  3.  * CategoricalValue.java
  4.  *
  5.  * Copyright: 2016 The Galactic Organization, France
  6.  *
  7.  * License: http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html CeCILL-B license
  8.  *
  9.  * This file is part of java-lattices.
  10.  * You can redistribute it and/or modify it under the terms of the CeCILL-B license.
  11.  */
  12. /**
  13.  * Categorical Value.
  14.  */
  15. public final class CategoricalValue {

  16.     /**
  17.      * Data.
  18.      */
  19.     private Object data;

  20.     /**
  21.      * Attribute.
  22.      */
  23.     private final CategoricalAttribute attribute;

  24.     /**
  25.      * Create a new CategoricalValue.
  26.      *
  27.      * @param data      Object
  28.      * @param attribute CategoricalAttribute
  29.      *
  30.      * @return a new CategoricalValue
  31.      */
  32.     static CategoricalValue create(final Object data, final CategoricalAttribute attribute) {
  33.         return new CategoricalValue(data, attribute);
  34.     }

  35.     /**
  36.      * This class is nod designed to be publicly instantiated.
  37.      *
  38.      * @param object    Object
  39.      * @param attribute CategoricalAttribute
  40.      */
  41.     private CategoricalValue(final Object object, final CategoricalAttribute attribute) {
  42.         this.setData(object);
  43.         this.attribute = attribute;
  44.     }

  45.     /**
  46.      * Get the underlying attribute.
  47.      *
  48.      * @return the underlying attribute
  49.      */
  50.     public CategoricalAttribute getAttribute() {
  51.         return this.attribute;
  52.     }

  53.     /**
  54.      * Get the underlying model.
  55.      *
  56.      * @return the underlying model
  57.      */
  58.     public CategoricalModel getModel() {
  59.         return this.attribute.getModel();
  60.     }

  61.     /**
  62.      * Get the underlying data.
  63.      *
  64.      * @return the underlying data
  65.      */
  66.     public Object getData() {
  67.         return this.data;
  68.     }

  69.     /**
  70.      * Set the underlying data.
  71.      *
  72.      * @param data the underlying data
  73.      *
  74.      * @return this for chaining
  75.      */
  76.     public CategoricalValue setData(final Object data) {
  77.         this.data = data;
  78.         return this;
  79.     }

  80.     /**
  81.      * Get the index of this value relatively to its model.
  82.      *
  83.      * @return the index of this value
  84.      */
  85.     public int index() {
  86.         return this.attribute.startIndex() + this.attribute.indexOf(this);
  87.     }

  88.     /**
  89.      * Returns a string representation of the data.
  90.      *
  91.      * @return a string representation of the data.
  92.      */
  93.     @Override
  94.     public String toString() {
  95.         final String string = this.data.toString();
  96.         if (string.contains(" ") || string.contains("\"")) {
  97.             return "\"" + string.replace("\"", "\\\"") + "\"";
  98.         } else {
  99.             return string;
  100.         }
  101.     }
  102. }