Skip to main content
summaryrefslogtreecommitdiffstats
blob: d8f5b4480ced63407b4a21d9b23596195d958bb9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * Copyright (c) 2010-2013 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Simon McDuff - initial API and implementation
 *    Ibrahim Sallam - code refactoring for CDO 3.0
 */
package org.eclipse.emf.cdo.server.internal.objectivity.schema;

import com.objy.as.app.Class_Object;
import com.objy.as.app.Numeric_Value;
import com.objy.as.app.VArray_Object;
import com.objy.db.app.ooId;

/**
 * @author Simon McDuff
 */
public abstract class ObjyArrayList<T>
{
  final static String sizeName = "elementCount";

  final static String arrayName = "curr";

  protected Class_Object classObject;

  private VArray_Object vArray;

  protected transient long cacheSize;

  transient long position;

  public static void initObject(Class_Object classObject)
  {
    // set the size to 0;
    classObject.nset_numeric(sizeName, new Numeric_Value(0));
  }

  public ObjyArrayList(Class_Object classObject)
  {
    this.classObject = classObject;
    this.cacheSize = -1;

  }

  public ooId getID()
  {
    return classObject.objectID();
  }

  // /**
  // * TODO - verify need.
  // */
  // private Class_Object getClassObject()
  // {
  // return classObject;
  // }

  public void clear()
  {
    getVArray().resize(0);
    cacheSize = 0;
    saveSize();
  }

  private void shiftRight(int index)
  {
    shiftRight(index, 1);
  }

  private void shiftRight(int index, int sizeToShift)
  {
    long size = this.cachedSize();

    for (long i = size - 1; i >= index; i--)
    {
      setValue(i + sizeToShift, getValue(i));
    }

    cacheSize += sizeToShift;
    saveSize();
  }

  private void shiftLeft(int index)
  {
    long size = this.cachedSize();
    for (long i = index; i < size - 1; i++)
    {
      setValue(i, getValue(i + 1));
    }

    cacheSize--;
    saveSize();
  }

  /**
   *
   */
  protected void grow(int item)
  {
    getVArray().resize(getVArraySize() + Math.max(item + 10, 10));
  }

  /**
   *
   */
  private void prepareToInsert(int numberToAdd)
  {
    long size = cachedSize();
    update();

    if (size + numberToAdd > getVArraySize())
    {
      grow(numberToAdd);
    }
  }

  protected long getVArraySize()
  {
    return getVArray().size();
  }

  protected void update()
  {
    getVArray().update();
  }

  protected VArray_Object getVArray()
  {
    if (vArray == null)
    {
      vArray = classObject.nget_varray(arrayName);
    }
    return vArray;
  }

  public void add(int index, T newValue)
  {
    prepareToInsert(1);
    shiftRight(index);
    basicSet(index, newValue);
  }

  public void addAll(int index, Object[] newValue)
  {
    prepareToInsert(newValue.length);
    shiftRight(index, newValue.length);

    for (int i = 0; i < newValue.length; i++)
    {
      @SuppressWarnings("unchecked")
      T value = (T)newValue[i];
      basicSet(index + i, value);
    }
  }

  public void remove(int index)
  {
    shiftLeft(index);
  }

  public void add(T newValue)
  {
    long size = cachedSize();

    prepareToInsert(1);
    setValue(size, newValue);
    cacheSize++;
    saveSize();
  }

  public void set(long index, T newValue)
  {
    basicSet(index, newValue);
    // cacheSize = -1;
  }

  public void move(long newPosition, long oldPosition)
  {
    if (oldPosition == newPosition)
    {
      return;
    }

    // get the object at oldPosition.
    T value = getValue(oldPosition);
    // remove the oldPosition.
    remove((int)oldPosition);
    // make a space at the newPosition by shifting elements
    shiftRight((int)newPosition);
    set(newPosition, value);
  }

  protected void basicSet(long index, T newValue)
  {
    if (index >= cachedSize())
    {
      throw new ArrayIndexOutOfBoundsException();
    }

    update();

    setValue(index, newValue);
  }

  public T get(long index)
  {
    if (index >= size())
    {
      throw new ArrayIndexOutOfBoundsException();
    }

    return getValue(index);
  }

  protected abstract void setValue(long index, T newValue);

  protected abstract T getValue(long index);

  protected void saveSize()
  {
    // System.out.println(">>> classObject: " + classObject.objectID().getStoreString() + " <<<");
    // System.out.println("ooArrayList.saveSize() - value to store in objy is: " + cacheSize);
    classObject.nset_numeric(sizeName, new Numeric_Value(cacheSize));
    resetCachedSize();
  }

  protected void resetCachedSize()
  {
    cacheSize = -1;
  }

  protected long cachedSize()
  {
    if (cacheSize == -1)
    {
      cacheSize = classObject.nget_numeric(sizeName).longValue();
      // System.out.println(">>> classObject: " + classObject.objectID().getStoreString() + " <<<");
      // System.out.println("ooArrayList.privateSize() - cacheSize was -1, value from objy is: " + cacheSize);
    }
    return cacheSize;
  }

  public long size()
  {
    // System.out.println(">>> classObject: " + classObject.objectID().getStoreString() + " <<<");
    // Numeric_Value nValue = classObject.nget_numeric(sizeName);
    // System.out.println("ooArrayList.size() - nValue: " + nValue.toString());
    // return classObject.nget_numeric(sizeName).longValue();
    return cachedSize();
  }

}

Back to the top