Advertisement
Guest User

Tuple.java

a guest
Jul 30th, 2014
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.89 KB | None | 0 0
  1. package crossnum;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. /**
  7.  * Represents a list of variable values that must sum to some required value.
  8.  *
  9.  * @author dub_nerd
  10.  *
  11.  */
  12. public class Tuple {
  13.    
  14.     /**
  15.      * List of variables.
  16.      */
  17.     private List<Variable> variables = new ArrayList<Variable>();
  18.    
  19.     /**
  20.      * The required sum.
  21.      */
  22.     private int requiredSum;
  23.    
  24.     /**
  25.      * Lock variables so that they are "owned" by this tuple. Anything not
  26.      * already locked becomes locked.
  27.      *
  28.      * @return the number of locks obtained.
  29.      */
  30.     int lock() {
  31.         int locks = 0;
  32.         for (Variable v : variables()) {
  33.             if (!v.isLocked()) {
  34.                 v.lock(this);
  35.                 locks++;
  36.             }
  37.         }
  38.         return locks;
  39.     }
  40.    
  41.    
  42.     /**
  43.      * Unlock variables "owned" by this tuple.
  44.      *
  45.      * @return the number of locks released.
  46.      */
  47.     int unlock() {
  48.         int locks = 0;
  49.         for (Variable v : variables()) {
  50.             if (v.getLock() == this) {
  51.                 v.lock(null);
  52.                 locks++;
  53.             }
  54.         }
  55.         return locks;
  56.     }
  57.    
  58.    
  59.     /**
  60.      * Return the number of variables in tuple which are not yet locked
  61.      * @return number unlocked
  62.      */
  63.     int getNumUnlocked() {
  64.         return (int)variables.stream().filter(v -> !v.isLocked()).count();
  65.     }
  66.  
  67.    
  68.     /**
  69.      * Get the value to which all the remaining unlocked unknowns must sum.
  70.      * @return remainder sum
  71.      */
  72.     int getRemainderSum() {
  73.        
  74.         // Get the total of all our unknowns that are already locked
  75.         int lockedTotal = variables().stream().filter(v -> v.isLocked())
  76.                 .mapToInt(v -> v.getValue()).sum();
  77.        
  78.         // Subtract from total required sum to get remainder sum
  79.         int remainderSum = getRequiredSum() - lockedTotal;
  80.         return remainderSum;
  81.     }
  82.  
  83.     // Other accessor methods
  84.  
  85.     public List<Variable> variables() {
  86.         return variables;
  87.     }
  88.    
  89.    
  90.     public int getRequiredSum() {
  91.         return requiredSum;
  92.     }
  93.    
  94.    
  95.     public void setRequiredSum(int requiredSum) {
  96.         this.requiredSum = requiredSum;
  97.     }
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement