Coverage Report - com.sdmetrics.math.IntegerMatrix - www.sdmetrics.com
 
Classes in this File Line Coverage Branch Coverage Complexity
IntegerMatrix
100%
19/19
100%
4/4
1,8
 
 1  
 /*
 2  
  * SDMetrics Open Core for UML design measurement
 3  
  * Copyright (c) Juergen Wuest
 4  
  * To contact the author, see <http://www.sdmetrics.com/Contact.html>.
 5  
  * 
 6  
  * This file is part of the SDMetrics Open Core.
 7  
  * 
 8  
  * SDMetrics Open Core is free software: you can redistribute it and/or modify
 9  
  * it under the terms of the GNU Affero General Public License as
 10  
  * published by the Free Software Foundation, either version 3 of the
 11  
  * License, or (at your option) any later version.
 12  
     
 13  
  * SDMetrics Open Core is distributed in the hope that it will be useful,
 14  
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 15  
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 16  
  * GNU Affero General Public License for more details.
 17  
  *
 18  
  * You should have received a copy of the GNU Affero General Public License
 19  
  * along with SDMetrics Open Core.  If not, see <http://www.gnu.org/licenses/>.
 20  
  *
 21  
  */
 22  
 package com.sdmetrics.math;
 23  
 
 24  
 import java.util.HashMap;
 25  
 
 26  
 /**
 27  
  * Realizes a sparse matrix of integers.
 28  
  * <p>
 29  
  * This implementation stores the coordinates and values of non-null entries in
 30  
  * a hash map. Hence, memory consumption depends on the number of non-null
 31  
  * entries in the matrix rather than the dimension of the matrix.
 32  
  */
 33  
 public class IntegerMatrix {
 34  
         /** Constant for 0. */
 35  2
         private static final Integer ZERO = Integer.valueOf(0);
 36  
         /** Constant for 1. */
 37  2
         private static final Integer ONE = Integer.valueOf(1);
 38  
 
 39  
         /**
 40  
          * HashMap to store the non-null values. Key is the coordinate pair, value
 41  
          * is the integer value at those coordinates.
 42  
          */
 43  
         private final HashMap<Long, Integer> map;
 44  
 
 45  
         /** Creates a new null matrix. */
 46  4
         public IntegerMatrix() {
 47  4
                 map = new HashMap<Long, Integer>();
 48  4
         }
 49  
 
 50  
         /**
 51  
          * Increments the integer value at the given coordinates by one.
 52  
          * 
 53  
          * @param row Matrix row number. Must not be negative.
 54  
          * @param col Matrix column number. Must not be negative.
 55  
          * @return The new value at the given coordinates.
 56  
          */
 57  
         public Integer increment(int row, int col) {
 58  11
                 Long coordinates = getCoordinatesKey(row, col);
 59  11
                 Integer oldValue = map.get(coordinates);
 60  11
                 if (oldValue == null) {
 61  10
                         map.put(coordinates, ONE);
 62  10
                         return ONE;
 63  
                 }
 64  
 
 65  1
                 Integer newValue = Integer.valueOf(oldValue.intValue() + 1);
 66  1
                 map.put(coordinates, newValue);
 67  1
                 return newValue;
 68  
         }
 69  
 
 70  
         /**
 71  
          * Retrieves the integer at the given coordinates.
 72  
          * 
 73  
          * @param row Matrix row number. Must not be negative.
 74  
          * @param col Matrix column number. Must not be negative.
 75  
          * @return Value at those coordinates.
 76  
          */
 77  
         public Integer get(int row, int col) {
 78  7
                 Integer result = map.get(getCoordinatesKey(row, col));
 79  7
                 if (result == null)
 80  3
                         return ZERO;
 81  4
                 return result;
 82  
         }
 83  
 
 84  
         /**
 85  
          * Tests whether this matrix is a null matrix.
 86  
          * 
 87  
          * @return <code>false</code> if the matrix has any non-zero entries, else
 88  
          *         <code>true</code>.
 89  
          */
 90  
         public boolean isEmpty() {
 91  3
                 return map.isEmpty();
 92  
         }
 93  
 
 94  
         private Long getCoordinatesKey(int row, int col) {
 95  
                 // put the column in the upper 4 bytes of a long, the rows in the lower
 96  
                 // 4 bytes
 97  18
                 return Long.valueOf((((long) col) << 32) | row);
 98  
         }
 99  
 }