Coverage Report - eu.simuline.octave.type.OctaveCell
 
Classes in this File Line Coverage Branch Coverage Complexity
OctaveCell
85%
17/20
83%
5/6
1.571
 
 1  
 /*
 2  
  * Copyright 2007, 2008 Ange Optimization ApS
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  */
 16  
 /**
 17  
  * @author Kim Hansen
 18  
  */
 19  
 package eu.simuline.octave.type;
 20  
 
 21  
 import eu.simuline.octave.exception.OctaveClassCastException;
 22  
 import eu.simuline.octave.type.cast.Cast;
 23  
 import eu.simuline.octave.type.matrix.AbstractGenericMatrix;
 24  
 import eu.simuline.octave.type.matrix.AbstractObjectMatrix;
 25  
 
 26  
 import it.unimi.dsi.fastutil.objects.ObjectArrayList;
 27  
 
 28  
 import java.util.List;
 29  
 
 30  
 /**
 31  
  * Nd cells. 
 32  
  */
 33  2
 public final class OctaveCell 
 34  
     extends AbstractObjectMatrix<OctaveObject> implements OctaveObject {
 35  
 
 36  
     /**
 37  
      * For some reason, the default value is not stored as such, 
 38  
      * but as a <code>null</code> value. 
 39  
      * Thus both in methods {@link #set(OctaveObject, int[])} 
 40  
      * and in {@link #get(int[])}, the value must be reconstructed. 
 41  
      * **** The reason for that, i cannot figure out. 
 42  
      * Instead I would pressume, that <code>null</code> 
 43  
      * is translated into <code>[]</code>, 
 44  
      * as elsewhere in octave's java-interface. 
 45  
      */
 46  2
     private static final OctaveObject DEFAULT_VALUE = new OctaveDouble(0, 0);
 47  
 
 48  
     /**
 49  
      * Warn about usage of old constructor. 
 50  
      * 
 51  
      * @deprecated use: new OctaveCell(0, 0)
 52  
      */
 53  
     @Deprecated
 54  
     public OctaveCell() {
 55  0
         super();
 56  0
     }
 57  
 
 58  
     /**
 59  
      * @param size
 60  
      */
 61  
     public OctaveCell(final int... size) {
 62  298
         super(size);
 63  298
     }
 64  
 
 65  
     private OctaveCell(final AbstractGenericMatrix<OctaveObject[], ObjectArrayList<OctaveObject>> o) {
 66  2
         super(o);
 67  2
     }
 68  
 
 69  
     @Override
 70  
     @SuppressWarnings("PMD.AvoidThrowingNullPointerException")
 71  
     public void set(OctaveObject value, int... pos) {
 72  352
         if (value == null) {
 73  
             // **** I would pressume that one should put [] instead. 
 74  0
             throw new NullPointerException("Cannot put null into OctaveCell");
 75  
         }
 76  352
         if (DEFAULT_VALUE.equals(value)) {
 77  14
             super.set(null, pos);
 78  
         } else {
 79  338
             super.set(value, pos);
 80  
         }
 81  352
     }
 82  
 
 83  
     @Override
 84  
     public OctaveObject get(int... pos) {
 85  368
         OctaveObject get = super.get(pos);
 86  368
          if (get == null) {
 87  54
             get = DEFAULT_VALUE;
 88  
         }
 89  368
         return get.shallowCopy();
 90  
 
 91  
     }
 92  
 
 93  
     /**
 94  
      * @param <T>
 95  
      * @param pos
 96  
      * @param castClass
 97  
      *            Class to cast to
 98  
      * @return shallow copy of value for this key.
 99  
      * @throws OctaveClassCastException
 100  
      *             if the object can not be cast to a castClass
 101  
      */
 102  
     public <T extends OctaveObject> T get(final Class<T> castClass,
 103  
                                           final int... pos) {
 104  24
         return Cast.cast(castClass, get(pos));
 105  
     }
 106  
 
 107  
     @Override
 108  
     public OctaveCell shallowCopy() {
 109  2
         return new OctaveCell(this);
 110  
     }
 111  
 
 112  
 }