Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalStackFrame.java @ 11c187c0

1
///////////////////////////////////////////////////////////////////////////////////////////////////
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
import org.distorted.library.effect.EffectType;
23

    
24
import java.util.ArrayList;
25
import java.util.HashMap;
26
import java.util.LinkedList;
27

    
28
///////////////////////////////////////////////////////////////////////////////////////////////////
29
/**
30
 * Implements a single 'frame' of all varaibles needed to remember the internal state of the library.
31
 * Such a frame must be remembered in a list whenever current Activity using the library fires off
32
 * another Activity which also wants to use the library. When that happens, we create a new 'frame',
33
 * remember the old one. When the second Activity ends and we come back to the first, we destroy the
34
 * second frame and recall the first.
35
 * <p>
36
 * Not part of public API, do not document
37
 *
38
 * @y.exclude
39
 */
40
public class InternalStackFrame
41
{
42
  private static class Job
43
    {
44
    InternalObject object;
45
    int action;
46

    
47
    Job(InternalObject o, int a)
48
      {
49
      object = o;
50
      action = a;
51
      }
52
    }
53

    
54
  private static LinkedList<InternalObject> mCommonDoneList = new LinkedList<>(); //
55
  private static HashMap<Long,Job> mCommonToDoMap           = new HashMap<>();    // Common
56
  private static long mCommonNextClientID                   = 0;                  // InternalObject
57
  private static long mCommonNextSystemID                   = 0;                  // (postprocessing)
58

    
59
  //////////////////////////////////////////////////////////////////
60
  private LinkedList<InternalObject> mDoneList;                   //
61
  private HashMap<Long,Job> mToDoMap;                             //
62
  private long mNextClientID;                                     // InternalObject
63
  private long mNextSystemID;                                     //
64
  private long mTaskId;                                           //
65

    
66
  //////////////////////////////////////////////////////////////////
67
  private boolean mInitialized;                                   // DistortedLibrary
68

    
69
  //////////////////////////////////////////////////////////////////
70
  private int[] mMax;                                             // EffectQueue
71

    
72
  //////////////////////////////////////////////////////////////////
73
  private long mNextEffectsID;                                    // DistortedEffects;
74

    
75
  //////////////////////////////////////////////////////////////////
76
  private long mNextEffectID;                                     // Effect;
77

    
78
  //////////////////////////////////////////////////////////////////
79
  private HashMap<ArrayList<Long>, InternalNodeData> mMapNodeID;  // InternalNodeData
80
  private long mNextNodeID;
81

    
82
  //////////////////////////////////////////////////////////////////
83
  private ArrayList<InternalMaster.Slave> mSlaves;                // InternalMaster
84

    
85
  ////////////////////////////////////////////////////////////////// EffectQueue
86
  private long mNextQueueID;                                      //
87
  private HashMap<ArrayList<Long>,Long> mMapID;                   // maps lists of Effect IDs (longs)
88
                                                                  // to a single long - the queue ID.
89

    
90
///////////////////////////////////////////////////////////////////////////////////////////////////
91

    
92
  InternalStackFrame(long taskID)
93
    {
94
    mDoneList     = new LinkedList<>();
95
    mToDoMap      = new HashMap<>();
96
    mMapNodeID    = new HashMap<>();
97
    mSlaves       = new ArrayList<>();
98
    mMapID        = new HashMap<>();
99
    mNextEffectsID= 0;
100
    mNextClientID = 0;
101
    mNextSystemID = 0;
102
    mNextNodeID   = 0;
103
    mNextQueueID  = 1;
104
    mTaskId       = taskID;
105
    mMax          = new int[EffectType.LENGTH];
106

    
107
    EffectType.reset(mMax);
108
    }
109

    
110
///////////////////////////////////////////////////////////////////////////////////////////////////
111

    
112
  long getTaskId()
113
    {
114
    return mTaskId;
115
    }
116

    
117
///////////////////////////////////////////////////////////////////////////////////////////////////
118

    
119
  static void onPauseCommon()
120
    {
121
    onPauseGeneric(mCommonDoneList,mCommonToDoMap);
122
    }
123

    
124
///////////////////////////////////////////////////////////////////////////////////////////////////
125

    
126
  void onPause()
127
    {
128
    onPauseGeneric(mDoneList,mToDoMap);
129
    }
130

    
131
///////////////////////////////////////////////////////////////////////////////////////////////////
132

    
133
  static void onPauseGeneric(LinkedList<InternalObject> list, HashMap<Long,Job> map)
134
    {
135
    int num = list.size();
136

    
137
    try
138
      {
139
      for (int i=0; i<num; i++)
140
        {
141
        InternalObject object = list.removeFirst();
142
        Job job = new Job(object, InternalObject.JOB_CREATE);
143
        map.put(object.getID(),job);
144
        object.recreate();
145
        }
146
      }
147
    catch( Exception ignored )
148
      {
149
      // something else removed an object in the meantime; ignore
150
      }
151
    }
152

    
153
///////////////////////////////////////////////////////////////////////////////////////////////////
154

    
155
  void toDo()
156
    {
157
    toDoGeneric(mDoneList,mToDoMap);
158
    }
159

    
160
///////////////////////////////////////////////////////////////////////////////////////////////////
161

    
162
  static void toDoCommon()
163
    {
164
    toDoGeneric(mCommonDoneList,mCommonToDoMap);
165
    }
166

    
167
///////////////////////////////////////////////////////////////////////////////////////////////////
168

    
169
  static void toDoGeneric(LinkedList<InternalObject> list, HashMap<Long,Job> map)
170
    {
171
    for(Long key: map.keySet())
172
      {
173
      Job job = map.get(key);
174
      InternalObject object = job.object;
175

    
176
      if( job.action==InternalObject.JOB_CREATE )
177
        {
178
        object.create();
179
        list.add(object);
180
        }
181
      else if( job.action==InternalObject.JOB_DELETE )
182
        {
183
        object.delete();
184
        }
185
      }
186

    
187
    map.clear();
188
    }
189

    
190
///////////////////////////////////////////////////////////////////////////////////////////////////
191

    
192
  static void cleanCommon()
193
    {
194
    mCommonDoneList.clear();
195
    mCommonToDoMap.clear();
196
    mCommonNextClientID = 0;
197
    mCommonNextSystemID = 0;
198
    }
199

    
200
///////////////////////////////////////////////////////////////////////////////////////////////////
201

    
202
  long generateID(int type, int storage)
203
    {
204
    if( storage==InternalObject.STORAGE_PRIVATE )
205
      {
206
      return type==InternalObject.TYPE_SYST ? --mNextSystemID : ++mNextClientID;
207
      }
208
    else
209
      {
210
      return type==InternalObject.TYPE_SYST ? --mCommonNextSystemID : ++mCommonNextClientID;
211
      }
212
    }
213

    
214
///////////////////////////////////////////////////////////////////////////////////////////////////
215

    
216
  void addToDoneList(InternalObject obj, int storage)
217
    {
218
    if( storage==InternalObject.STORAGE_PRIVATE )
219
      {
220
      if( !mDoneList.contains(obj) )
221
        {
222
        mDoneList.add(obj);
223
        }
224
      }
225
    else
226
      {
227
      if( !mCommonDoneList.contains(obj) ) mCommonDoneList.add(obj);
228
      }
229
    }
230

    
231
///////////////////////////////////////////////////////////////////////////////////////////////////
232

    
233
  void removeFromDoneList(InternalObject obj, int storage)
234
    {
235
    if( storage==InternalObject.STORAGE_PRIVATE )
236
      {
237
      mDoneList.remove(obj);
238
      }
239
    else
240
      {
241
      mCommonDoneList.remove(obj);
242
      }
243
    }
244

    
245
///////////////////////////////////////////////////////////////////////////////////////////////////
246

    
247
  void markFor(InternalObject obj, long id, int storage, int jobType)
248
    {
249
    if( storage==InternalObject.STORAGE_PRIVATE )
250
      {
251
      mDoneList.remove(obj);
252
      mToDoMap.put(id, new Job(obj,jobType) );
253
      }
254
    else
255
      {
256
      mCommonDoneList.remove(obj);
257
      mCommonToDoMap.put(id, new Job(obj,jobType) );
258
      }
259
    }
260

    
261
///////////////////////////////////////////////////////////////////////////////////////////////////
262

    
263
  boolean isInitialized()
264
    {
265
    return mInitialized;
266
    }
267

    
268
///////////////////////////////////////////////////////////////////////////////////////////////////
269

    
270
  void setInitialized(boolean init)
271
    {
272
    mInitialized = init;
273
    }
274

    
275
///////////////////////////////////////////////////////////////////////////////////////////////////
276

    
277
  long getNextEffectsID()
278
    {
279
    return ++mNextEffectsID;
280
    }
281

    
282
///////////////////////////////////////////////////////////////////////////////////////////////////
283

    
284
  long getNextEffectID()
285
    {
286
    return mNextEffectID++;
287
    }
288

    
289
///////////////////////////////////////////////////////////////////////////////////////////////////
290

    
291
  int getMax(int index)
292
    {
293
    return mMax[index];
294
    }
295

    
296
///////////////////////////////////////////////////////////////////////////////////////////////////
297

    
298
  boolean setMax(int index, int max)
299
    {
300
    if( !mInitialized || max<mMax[index] )
301
      {
302
      mMax[index] = max;
303
      return true;
304
      }
305

    
306
    return false;
307
    }
308

    
309
///////////////////////////////////////////////////////////////////////////////////////////////////
310

    
311
  public HashMap<ArrayList<Long>,Long> getMap()
312
    {
313
    return mMapID;
314
    }
315

    
316
///////////////////////////////////////////////////////////////////////////////////////////////////
317

    
318
  public long getNextQueueID()
319
    {
320
    mNextQueueID++;
321

    
322
    return mNextQueueID-1;
323
    }
324

    
325
///////////////////////////////////////////////////////////////////////////////////////////////////
326

    
327
  InternalNodeData getMapID(ArrayList<Long> key)
328
    {
329
    return mMapNodeID.get(key);
330
    }
331

    
332
///////////////////////////////////////////////////////////////////////////////////////////////////
333

    
334
  InternalNodeData putNewDataToMap(ArrayList<Long> key)
335
    {
336
    InternalNodeData data = new InternalNodeData(++mNextNodeID,key);
337
    mMapNodeID.put(key,data);
338
    return data;
339
    }
340

    
341
///////////////////////////////////////////////////////////////////////////////////////////////////
342

    
343
  void removeKeyFromMap(ArrayList<Long> key)
344
    {
345
    mMapNodeID.remove(key);
346
    }
347

    
348
///////////////////////////////////////////////////////////////////////////////////////////////////
349

    
350
  ArrayList<InternalMaster.Slave> getSet()
351
    {
352
    return mSlaves;
353
    }
354

    
355
///////////////////////////////////////////////////////////////////////////////////////////////////
356

    
357
  void debugLists(String frameMarker)
358
    {
359
    debugListsGeneric(mDoneList,mToDoMap,frameMarker);
360
    }
361

    
362
///////////////////////////////////////////////////////////////////////////////////////////////////
363

    
364
  static void debugCommonList()
365
    {
366
    debugListsGeneric(mCommonDoneList,mCommonToDoMap,"Common");
367
    }
368

    
369
///////////////////////////////////////////////////////////////////////////////////////////////////
370

    
371
  static void debugListsGeneric(LinkedList<InternalObject> list, HashMap<Long,Job> map,String frameMarker)
372
    {
373
    android.util.Log.e("Object", frameMarker);
374
    android.util.Log.e("Object", "  Done list:");
375

    
376
    for(InternalObject object : list)
377
      {
378
      object.print("  ");
379
      }
380

    
381
    android.util.Log.e("Object", "  ToDo list:");
382

    
383
    Job job;
384

    
385
    for(Long key: map.keySet())
386
      {
387
      job = map.get(key);
388
      job.object.print(job.action==InternalObject.JOB_CREATE ? " create":" delete");
389
      }
390
    }
391

    
392
///////////////////////////////////////////////////////////////////////////////////////////////////
393

    
394
  void debugMap(String frameMarker)
395
    {
396
    android.util.Log.e("Object", frameMarker);
397
    InternalNodeData tmp;
398

    
399
    for(ArrayList<Long> key: mMapNodeID.keySet())
400
      {
401
      tmp = mMapNodeID.get(key);
402
      android.util.Log.e("NodeData", "NodeID: "+tmp.ID+" <-- "+key);
403
      }
404
    }
405
  }
(14-14/16)