Project

General

Profile

Download (12.3 KB) Statistics
| Branch: | Revision:

library / src / main / java / org / distorted / library / main / InternalStackFrame.java @ 8b36dabf

1 9ec374e8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// Distorted is free software: you can redistribute it and/or modify                             //
7
// it under the terms of the GNU General Public License as published by                          //
8
// the Free Software Foundation, either version 2 of the License, or                             //
9
// (at your option) any later version.                                                           //
10
//                                                                                               //
11
// Distorted is distributed in the hope that it will be useful,                                  //
12
// but WITHOUT ANY WARRANTY; without even the implied warranty of                                //
13
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                                 //
14
// GNU General Public License for more details.                                                  //
15
//                                                                                               //
16
// You should have received a copy of the GNU General Public License                             //
17
// along with Distorted.  If not, see <http://www.gnu.org/licenses/>.                            //
18
///////////////////////////////////////////////////////////////////////////////////////////////////
19
20
package org.distorted.library.main;
21
22 30094332 Leszek Koltunski
import org.distorted.library.effect.EffectType;
23
24 3543a3cf Leszek Koltunski
import java.util.ArrayList;
25 9ec374e8 Leszek Koltunski
import java.util.HashMap;
26 3bbe4d67 Leszek Koltunski
import java.util.HashSet;
27 9ec374e8 Leszek Koltunski
import java.util.LinkedList;
28 3bbe4d67 Leszek Koltunski
import java.util.Set;
29 9ec374e8 Leszek Koltunski
30
///////////////////////////////////////////////////////////////////////////////////////////////////
31
32
public class InternalStackFrame
33
{
34
  private static class Job
35
    {
36
    InternalObject object;
37
    int action;
38
39
    Job(InternalObject o, int a)
40
      {
41
      object = o;
42
      action = a;
43
      }
44
    }
45
46
  private static LinkedList<InternalObject> mCommonDoneList = new LinkedList<>(); //
47
  private static HashMap<Long,Job> mCommonToDoMap           = new HashMap<>();    // Common
48
  private static long mCommonNextClientID                   = 0;                  // InternalObject
49
  private static long mCommonNextSystemID                   = 0;                  // (postprocessing)
50
51 3543a3cf Leszek Koltunski
  //////////////////////////////////////////////////////////////////
52
  private LinkedList<InternalObject> mDoneList;                   //
53
  private HashMap<Long,Job> mToDoMap;                             //
54
  private long mNextClientID;                                     // InternalObject
55
  private long mNextSystemID;                                     //
56
  private long mTaskId;                                           //
57 9ec374e8 Leszek Koltunski
58 3543a3cf Leszek Koltunski
  //////////////////////////////////////////////////////////////////
59
  private boolean mInitialized;                                   // DistortedLibrary
60 30094332 Leszek Koltunski
61 3543a3cf Leszek Koltunski
  //////////////////////////////////////////////////////////////////
62
  private int[] mMax;                                             // EffectQueue
63
64
  //////////////////////////////////////////////////////////////////
65
  private long mNextEffectsID;                                    // DistortedEffects;
66
67
  //////////////////////////////////////////////////////////////////
68
  private long mNextEffectID;                                     // Effect;
69
70
  //////////////////////////////////////////////////////////////////
71
  private HashMap<ArrayList<Long>, InternalNodeData> mMapNodeID;  // InternalNodeData
72
  private long mNextNodeID;
73 9ec374e8 Leszek Koltunski
74 3bbe4d67 Leszek Koltunski
  //////////////////////////////////////////////////////////////////
75
  private Set<InternalMaster.Slave> mSlaves;                      // InternalMaster
76
77
  ////////////////////////////////////////////////////////////////// EffectQueue
78
  private long mNextQueueID;                                      //
79
  private HashMap<ArrayList<Long>,Long> mMapID;                   // maps lists of Effect IDs (longs)
80
                                                                  // to a single long - the queue ID.
81
82 9ec374e8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
83
84
  InternalStackFrame(long taskID)
85
    {
86
    mDoneList     = new LinkedList<>();
87
    mToDoMap      = new HashMap<>();
88 3543a3cf Leszek Koltunski
    mMapNodeID    = new HashMap<>();
89 3bbe4d67 Leszek Koltunski
    mSlaves       = new HashSet<>();
90
    mMapID        = new HashMap<>();
91 3543a3cf Leszek Koltunski
    mNextEffectsID= 0;
92 9ec374e8 Leszek Koltunski
    mNextClientID = 0;
93
    mNextSystemID = 0;
94 3543a3cf Leszek Koltunski
    mNextNodeID   = 0;
95 3bbe4d67 Leszek Koltunski
    mNextQueueID  = 1;
96 9ec374e8 Leszek Koltunski
    mTaskId       = taskID;
97 30094332 Leszek Koltunski
    mMax          = new int[EffectType.LENGTH];
98
99
    EffectType.reset(mMax);
100 9ec374e8 Leszek Koltunski
    }
101
102
///////////////////////////////////////////////////////////////////////////////////////////////////
103
104
  long getTaskId()
105
    {
106
    return mTaskId;
107
    }
108
109
///////////////////////////////////////////////////////////////////////////////////////////////////
110
111
  static void onPauseCommon()
112
    {
113
    onPauseGeneric(mCommonDoneList,mCommonToDoMap);
114
    }
115
116
///////////////////////////////////////////////////////////////////////////////////////////////////
117
118
  void onPause()
119
    {
120
    onPauseGeneric(mDoneList,mToDoMap);
121
    }
122
123
///////////////////////////////////////////////////////////////////////////////////////////////////
124
125
  static void onPauseGeneric(LinkedList<InternalObject> list, HashMap<Long,Job> map)
126
    {
127
    int num = list.size();
128
129
    try
130
      {
131
      for (int i=0; i<num; i++)
132
        {
133
        InternalObject object = list.removeFirst();
134
        Job job = new Job(object, InternalObject.JOB_CREATE);
135
        map.put(object.getID(),job);
136
        object.recreate();
137
        }
138
      }
139
    catch( Exception ignored )
140
      {
141
      // something else removed an object in the meantime; ignore
142
      }
143
    }
144
145
///////////////////////////////////////////////////////////////////////////////////////////////////
146
147
  void toDo()
148
    {
149
    toDoGeneric(mDoneList,mToDoMap);
150
    }
151
152
///////////////////////////////////////////////////////////////////////////////////////////////////
153
154
  static void toDoCommon()
155
    {
156
    toDoGeneric(mCommonDoneList,mCommonToDoMap);
157
    }
158
159
///////////////////////////////////////////////////////////////////////////////////////////////////
160
161
  static void toDoGeneric(LinkedList<InternalObject> list, HashMap<Long,Job> map)
162
    {
163
    for(Long key: map.keySet())
164
      {
165
      Job job = map.get(key);
166
      InternalObject object = job.object;
167
168
      if( job.action==InternalObject.JOB_CREATE )
169
        {
170
        object.create();
171
        list.add(object);
172
        }
173
      else if( job.action==InternalObject.JOB_DELETE )
174
        {
175
        object.delete();
176
        }
177
      }
178
179
    map.clear();
180
    }
181
182
///////////////////////////////////////////////////////////////////////////////////////////////////
183
184
  static void cleanCommon()
185
    {
186
    mCommonDoneList.clear();
187
    mCommonToDoMap.clear();
188
    mCommonNextClientID = 0;
189
    mCommonNextSystemID = 0;
190
    }
191
192
///////////////////////////////////////////////////////////////////////////////////////////////////
193
194
  long generateID(int type, int storage)
195
    {
196
    if( storage==InternalObject.STORAGE_PRIVATE )
197
      {
198
      return type==InternalObject.TYPE_SYST ? --mNextSystemID : ++mNextClientID;
199
      }
200
    else
201
      {
202
      return type==InternalObject.TYPE_SYST ? --mCommonNextSystemID : ++mCommonNextClientID;
203
      }
204
    }
205
206
///////////////////////////////////////////////////////////////////////////////////////////////////
207
208
  void addToDoneList(InternalObject obj, int storage)
209
    {
210
    if( storage==InternalObject.STORAGE_PRIVATE )
211
      {
212
      if( !mDoneList.contains(obj) )
213
        {
214
        mDoneList.add(obj);
215
        }
216
      }
217
    else
218
      {
219
      if( !mCommonDoneList.contains(obj) ) mCommonDoneList.add(obj);
220
      }
221
    }
222
223
///////////////////////////////////////////////////////////////////////////////////////////////////
224
225
  void removeFromDoneList(InternalObject obj, int storage)
226
    {
227
    if( storage==InternalObject.STORAGE_PRIVATE )
228
      {
229
      mDoneList.remove(obj);
230
      }
231
    else
232
      {
233
      mCommonDoneList.remove(obj);
234
      }
235
    }
236
237
///////////////////////////////////////////////////////////////////////////////////////////////////
238
239
  void markFor(InternalObject obj, long id, int storage, int jobType)
240
    {
241
    if( storage==InternalObject.STORAGE_PRIVATE )
242
      {
243
      mDoneList.remove(obj);
244
      mToDoMap.put(id, new Job(obj,jobType) );
245
      }
246
    else
247
      {
248
      mCommonDoneList.remove(obj);
249
      mCommonToDoMap.put(id, new Job(obj,jobType) );
250
      }
251
    }
252
253
///////////////////////////////////////////////////////////////////////////////////////////////////
254
255
  boolean isInitialized()
256
    {
257
    return mInitialized;
258
    }
259
260
///////////////////////////////////////////////////////////////////////////////////////////////////
261
262
  void setInitialized(boolean init)
263
    {
264
    mInitialized = init;
265
    }
266
267 3543a3cf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
268
269
  long getNextEffectsID()
270
    {
271
    return ++mNextEffectsID;
272
    }
273
274
///////////////////////////////////////////////////////////////////////////////////////////////////
275
276
  long getNextEffectID()
277
    {
278
    return mNextEffectID++;
279
    }
280
281 30094332 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
282
283
  int getMax(int index)
284
    {
285
    return mMax[index];
286
    }
287
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289
290
  boolean setMax(int index, int max)
291
    {
292
    if( !mInitialized || max<mMax[index] )
293
      {
294
      mMax[index] = max;
295
      return true;
296
      }
297
298
    return false;
299
    }
300
301 3bbe4d67 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
302
303
  public HashMap<ArrayList<Long>,Long> getMap()
304
    {
305
    return mMapID;
306
    }
307
308
///////////////////////////////////////////////////////////////////////////////////////////////////
309
310
  public long getNextQueueID()
311
    {
312
    mNextQueueID++;
313
314
    return mNextQueueID-1;
315
    }
316
317 3543a3cf Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
318
319
  InternalNodeData getMapID(ArrayList<Long> key)
320
    {
321
    return mMapNodeID.get(key);
322
    }
323
324
///////////////////////////////////////////////////////////////////////////////////////////////////
325
326
  InternalNodeData putNewDataToMap(ArrayList<Long> key)
327
    {
328
    InternalNodeData data = new InternalNodeData(++mNextNodeID,key);
329
    mMapNodeID.put(key,data);
330
    return data;
331
    }
332
333
///////////////////////////////////////////////////////////////////////////////////////////////////
334
335
  void removeKeyFromMap(ArrayList<Long> key)
336
    {
337
    mMapNodeID.remove(key);
338
    }
339
340 3bbe4d67 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
341
342
  Set<InternalMaster.Slave> getSet()
343
    {
344
    return mSlaves;
345
    }
346
347 9ec374e8 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
348
349
  void debugLists(String frameMarker)
350
    {
351
    debugListsGeneric(mDoneList,mToDoMap,frameMarker);
352
    }
353
354
///////////////////////////////////////////////////////////////////////////////////////////////////
355
356
  static void debugCommonList()
357
    {
358
    debugListsGeneric(mCommonDoneList,mCommonToDoMap,"Common");
359
    }
360
361
///////////////////////////////////////////////////////////////////////////////////////////////////
362
363
  static void debugListsGeneric(LinkedList<InternalObject> list, HashMap<Long,Job> map,String frameMarker)
364
    {
365
    android.util.Log.e("Object", frameMarker);
366
    android.util.Log.e("Object", "  Done list:");
367
368
    for(InternalObject object : list)
369
      {
370
      object.print("  ");
371
      }
372
373
    android.util.Log.e("Object", "  ToDo list:");
374
375
    Job job;
376
377
    for(Long key: map.keySet())
378
      {
379
      job = map.get(key);
380
      job.object.print(job.action==InternalObject.JOB_CREATE ? " create":" delete");
381
      }
382
    }
383 3543a3cf Leszek Koltunski
384
///////////////////////////////////////////////////////////////////////////////////////////////////
385
386
  void debugMap(String frameMarker)
387
    {
388
    android.util.Log.e("Object", frameMarker);
389
    InternalNodeData tmp;
390
391
    for(ArrayList<Long> key: mMapNodeID.keySet())
392
      {
393
      tmp = mMapNodeID.get(key);
394
      android.util.Log.e("NodeData", "NodeID: "+tmp.ID+" <-- "+key);
395
      }
396
    }
397 9ec374e8 Leszek Koltunski
  }