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 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 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 super();
56 }
57
58 /**
59 * @param size
60 */
61 public OctaveCell(final int... size) {
62 super(size);
63 }
64
65 private OctaveCell(final AbstractGenericMatrix<OctaveObject[], ObjectArrayList<OctaveObject>> o) {
66 super(o);
67 }
68
69 @Override
70 @SuppressWarnings("PMD.AvoidThrowingNullPointerException")
71 public void set(OctaveObject value, int... pos) {
72 if (value == null) {
73 // **** I would presume that one should put [] instead.
74 throw new NullPointerException("Cannot put null into OctaveCell");
75 }
76 if (DEFAULT_VALUE.equals(value)) {
77 super.set(null, pos);
78 } else {
79 super.set(value, pos);
80 }
81 }
82
83 @Override
84 public OctaveObject get(int... pos) {
85 OctaveObject get = super.get(pos);
86 if (get == null) {
87 get = DEFAULT_VALUE;
88 }
89 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 return Cast.cast(castClass, get(pos));
105 }
106
107 @Override
108 public OctaveCell shallowCopy() {
109 return new OctaveCell(this);
110 }
111
112 }