Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalStackFrame.java @ f7c72bd1

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