Advertisement
Guest User

Variable.java

a guest
Jul 30th, 2014
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.49 KB | None | 0 0
  1. package crossnum;
  2.  
  3. import java.util.Comparator;
  4.  
  5. /**
  6.  * Represents an integer variable value, with some special functionality to support
  7.  * the algorithm in CrossNum.
  8.  *
  9.  * @author dub_nerd
  10.  *
  11.  */
  12. public class Variable {
  13.  
  14.     /**
  15.      * The value of this variable.
  16.      */
  17.     private int value;
  18.    
  19.     /**
  20.      * An ordinal value which can function as a label for the variable.
  21.      */
  22.     private int ordinal = -1;
  23.    
  24.     /**
  25.      * Variable values may be locked against modification. If the
  26.      * lockHolder is non-null the variable is locked.
  27.      */
  28.     private Object lockHolder;
  29.  
  30.     /**
  31.      * A convenience Comparator for sorting by ordinal value
  32.      */
  33.     static public Comparator<Variable> sortByOrdinal = new Comparator<Variable>() {
  34.  
  35.         @Override
  36.         public int compare(Variable o1, Variable o2) {
  37.             return o1.getOrdinal() > o2.getOrdinal()? 1 : o1.getOrdinal() == o2.getOrdinal()? 0 : -1;
  38.         }
  39.     };
  40.    
  41.     /**
  42.      * Construct a new Variable.
  43.      *
  44.      * @param val
  45.      *            the variable value
  46.      */
  47.     public Variable(int val) {
  48.         value = val;
  49.     }
  50.    
  51.     // Various accessor methods
  52.    
  53.     public int getValue() {
  54.         return value;
  55.     }
  56.  
  57.  
  58.     public void setValue(int value) {
  59.         this.value = value;
  60.     }
  61.  
  62.  
  63.     public int getOrdinal() {
  64.         return ordinal;
  65.     }
  66.  
  67.    
  68.     public void setOrdinal(int ord) {
  69.         this.ordinal = ord;
  70.     }
  71.  
  72.  
  73.     public Object getLock() {
  74.         return lockHolder;
  75.     }
  76.  
  77.    
  78.     public boolean isLocked() {
  79.         return lockHolder != null;
  80.     }
  81.  
  82.    
  83.     public void lock(Object holder) {
  84.         lockHolder = holder;
  85.     }
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement