Project

General

Profile

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

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

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2020 Leszek Koltunski  leszek@koltunski.pl                                          //
3
//                                                                                               //
4
// This file is part of Distorted.                                                               //
5
//                                                                                               //
6
// 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
//                                                                                               //
11
// This library 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 GNU                             //
14
// Lesser General Public License for more details.                                               //
15
//                                                                                               //
16
// 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
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
package org.distorted.library.main;
22

    
23
import org.distorted.library.effect.EffectType;
24

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

    
29
///////////////////////////////////////////////////////////////////////////////////////////////////
30
/**
31
 * Implements a single 'frame' of all variables needed to remember the internal state of the library.
32
 * 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
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
  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

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

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

    
70
  //////////////////////////////////////////////////////////////////
71
  private final int[] mMax;                                       // EffectQueue
72

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

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

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

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

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

    
91
///////////////////////////////////////////////////////////////////////////////////////////////////
92

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

    
108
    EffectType.reset(mMax);
109
    }
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
///////////////////////////////////////////////////////////////////////////////////////////////////
277

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

    
283
///////////////////////////////////////////////////////////////////////////////////////////////////
284

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

    
290
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
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
///////////////////////////////////////////////////////////////////////////////////////////////////
350

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

    
356
///////////////////////////////////////////////////////////////////////////////////////////////////
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

    
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
  }
(14-14/17)