View Javadoc
1   /*
2    * Copyright 2009 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  package eu.simuline.octave.type.matrix;
17  
18  import eu.simuline.octave.util.StringUtil;
19  
20  import it.unimi.dsi.fastutil.objects.ObjectArrayList;
21  
22  import java.util.List;
23  import java.util.Objects;
24  
25  /**
26   * General matrix with Object values 
27   * which serves also as base class for 
28   * {@link eu.simuline.octave.type.OctaveCell}. 
29   * 
30   * @param <T>
31   *    Type of the values
32   */
33  // used as superclass of class OctaveCell only 
34  public abstract class AbstractObjectMatrix<T> 
35      extends AbstractGenericMatrix<T[], ObjectArrayList<T>> {
36  
37      /**
38       * @param size
39       */
40      // used in OctaveCell(int...), 
41      protected AbstractObjectMatrix(final int... size) {
42          super(size);
43      }
44  
45      /**
46       * @param dataA
47       * @param size
48       */
49      // used by end users 
50      @SuppressWarnings("unchecked")
51      protected AbstractObjectMatrix(final Object[] dataA, final int... size) {
52          super((T[]) dataA, size);
53      }
54  
55      /**
56       * Copy constructor. 
57       * 
58       * @param o
59       */
60      // used in OctaveCell(final AbstractGenericMatrix), 
61      protected AbstractObjectMatrix(final AbstractGenericMatrix<T[], ObjectArrayList<T>> o) {
62          super(o);
63      }
64  
65      // api-docs inherited from base class 
66      protected final ObjectArrayList<T> newL(final int size) {
67          ObjectArrayList<T> list = new ObjectArrayList<T>(size);
68  	list.size(size);
69  	return list;
70      }
71  
72      // api-docs inherited from base class 
73      protected final int initL(T[] data, final int size) {
74  	this.dataL = new ObjectArrayList<T>(data);
75  	this.dataL.size(size);
76  	return data.length;
77      }
78  
79      // api-docs inherited from base class 
80      protected T[] getDataA() {
81  	return this.dataL.elements();
82      }
83  
84      /**
85       * Set the value resizing by need. 
86       * 
87       * @param value
88       * @param pos
89       * @see #setPlain(Object, int)
90       */
91      // overwritten in OctaveCell 
92      // questionable: where is value==null needed? 
93      // maybe this can be excluded right here. 
94      // It is not worth fixing now because possibly redesign required. 
95      @SuppressWarnings("checkstyle:designforextension")
96      public void set(final T value, final int... pos) {
97          resizeUp(pos);
98          setPlain(value, pos2ind(pos));
99      }
100 
101     /**
102      * Set the value assuming resize is not necessary. 
103      * 
104      * @param value
105      * @param pos
106      * @see #set(Object, int[])
107      */
108     public final void setPlain(final T value, final int pos) {
109 	 this.dataL.set(pos, value);
110     }
111 
112     // api-docs inherited from AbstractGenericMatrix 
113     public final void setPlain(final String value, final int pos) {
114 	throw new UnsupportedOperationException();
115     }
116 
117     /**
118      * Get the value. 
119      * 
120      * @param pos
121      * @return value at pos
122      */
123     // overwritten in OctaveCell 
124     // see set(...)
125     @SuppressWarnings("checkstyle:designforextension")
126     public T get(final int... pos) {
127 	return this.dataL.get(pos2ind(pos));
128     }
129 
130     // **** may dataL be null??  
131     public final String getPlainString(int pos) {
132 	return StringUtil.toString(this.dataL.get(pos));
133     }
134 }