Advertisement
Guest User

Celine

a guest
Mar 2nd, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.68 KB | None | 0 0
  1.  
  2. import java.util.HashMap;
  3. import java.util.HashSet;
  4. import java.util.Map;
  5. import java.util.Set;
  6.  
  7. /**
  8.  * An implementation of {@link IntRelation} using a {@link Map} of {@link Sets}.
  9.  *
  10.  * @author s133269
  11.  */
  12. public class IntRelationMapOfSets extends IntRelation {
  13.  
  14.     /**
  15.      * Representation of the relation.
  16.      */
  17.     protected final Map<Integer, Set<Integer>> relation;
  18.  
  19.     /*
  20.      * Representation invariants
  21.      *
  22.      * NotNull: relation != null
  23.      * SetsNotNull: (\forall i; relation.containsKey(i); relation.get(i) != null),
  24.      *     where relation.containsKey(i) == 0 <= i < relation.size()
  25.      * SetsInExtent: (\forall i; relation.containsKey(i);
  26.      *     relation.get(i) subset of [0 .. relation.size()))
  27.      *
  28.      * Abstraction function
  29.      *
  30.      * AF(this) = set of (a, b) such that relation.get(a).contains(b)
  31.      */
  32.     public IntRelationMapOfSets(final int n) throws IllegalArgumentException {
  33.         super(n);
  34.         if (n < 0) {
  35.             throw new IllegalArgumentException(
  36.                     "IntRelationMapOfSets.constructor.pre violated:"
  37.                     + " n == " + n + " < 0");
  38.         } else {
  39.             relation = new HashMap<Integer, Set<Integer>>();
  40.             for (int i = 0; i < n; i++) {
  41.                 relation.put(i, new HashSet<Integer>());
  42.             }
  43.         }
  44.     }
  45.  
  46.     @Override
  47.     public boolean isRepOk() {
  48.         // NotNull
  49.         if (relation == null) {
  50.             throw new IllegalStateException("relation == null");
  51.         }
  52.         for (int i = 0; i < extent(); i++) {
  53.             final Set<Integer> set = relation.get(i);
  54.             // SetsNotNull
  55.             if (set == null) {
  56.                 throw new IllegalStateException(
  57.                         "relation.get(" + i + ") == null");
  58.             }
  59.             // SetsInExtent
  60.             for (int j = 0; j < set.size(); j++) {
  61.                 if (!(0 <= j && j < extent())) {
  62.                     throw new IllegalStateException(
  63.                             "relation.get(" + i + ") contains " + j);
  64.                 }
  65.             }
  66.         }
  67.         return true;
  68.     }
  69.  
  70.     @Override
  71.     public int extent() {
  72.         return relation.size();
  73.     }
  74.  
  75.     @Override
  76.     public boolean areRelated(int a, int b) throws IllegalArgumentException {
  77.         if (!isValidPair(a, b)) {
  78.             throw new IllegalArgumentException(
  79.                     "IntRelationMapOfSets.areRelated.pre violated:"
  80.                     + a + " or " + b + " or both are not"
  81.                     + " within the range [0... " + relation.size() + ")");
  82.         }
  83.         if (relation.containsKey(a) == false) {
  84.             return false;
  85.         }
  86.         else if (relation.get(a) == null){
  87.             return false;
  88.         }
  89.         return relation.get(a).contains(b);
  90.     }
  91.  
  92.     @Override
  93.     public void add(int a, int b) throws IllegalArgumentException {
  94.         if (!isValidPair(a, b)) {
  95.             throw new IllegalArgumentException(
  96.                     "IntRelationMapOfSets.add.pre violated:"
  97.                     + a + " or " + b + " or both are not"
  98.                     + " within the range [0... " + relation.size() + ")");
  99.         } else {
  100.             relation.get(a).add(b);
  101.         }
  102.     }
  103.  
  104.     @Override
  105.     public void remove(int a, int b) throws IllegalArgumentException {
  106.         if (!isValidPair(a, b)) {
  107.             throw new IllegalArgumentException(
  108.                     "IntRelationMapOfSets.add.pre violated:"
  109.                     + a + " or " + b + " or both are not"
  110.                     + " within the range [0... " + relation.size() + ")");
  111.         } else {
  112.             relation.get(a).remove(b);
  113.         }
  114.     }
  115.  
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement