Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalStackFrame.java @ 97b6c85e

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
public class InternalStackFrame
31
{
32
  private static class Job
33
    {
34
    InternalObject object;
35
    int action;
36

    
37
    Job(InternalObject o, int a)
38
      {
39
      object = o;
40
      action = a;
41
      }
42
    }
43

    
44
  private static LinkedList<InternalObject> mCommonDoneList = new LinkedList<>(); //
45
  private static HashMap<Long,Job> mCommonToDoMap           = new HashMap<>();    // Common
46
  private static long mCommonNextClientID                   = 0;                  // InternalObject
47
  private static long mCommonNextSystemID                   = 0;                  // (postprocessing)
48

    
49
  //////////////////////////////////////////////////////////////////
50
  private LinkedList<InternalObject> mDoneList;                   //
51
  private HashMap<Long,Job> mToDoMap;                             //
52
  private long mNextClientID;                                     // InternalObject
53
  private long mNextSystemID;                                     //
54
  private long mTaskId;                                           //
55

    
56
  //////////////////////////////////////////////////////////////////
57
  private boolean mInitialized;                                   // DistortedLibrary
58

    
59
  //////////////////////////////////////////////////////////////////
60
  private int[] mMax;                                             // EffectQueue
61

    
62
  //////////////////////////////////////////////////////////////////
63
  private long mNextEffectsID;                                    // DistortedEffects;
64

    
65
  //////////////////////////////////////////////////////////////////
66
  private long mNextEffectID;                                     // Effect;
67

    
68
  //////////////////////////////////////////////////////////////////
69
  private HashMap<ArrayList<Long>, InternalNodeData> mMapNodeID;  // InternalNodeData
70
  private long mNextNodeID;
71

    
72
  //////////////////////////////////////////////////////////////////
73
  private ArrayList<InternalMaster.Slave> mSlaves;                // InternalMaster
74

    
75
  ////////////////////////////////////////////////////////////////// EffectQueue
76
  private long mNextQueueID;                                      //
77
  private HashMap<ArrayList<Long>,Long> mMapID;                   // maps lists of Effect IDs (longs)
78
                                                                  // to a single long - the queue ID.
79

    
80
///////////////////////////////////////////////////////////////////////////////////////////////////
81

    
82
  InternalStackFrame(long taskID)
83
    {
84
    mDoneList     = new LinkedList<>();
85
    mToDoMap      = new HashMap<>();
86
    mMapNodeID    = new HashMap<>();
87
    mSlaves       = new ArrayList<>();
88
    mMapID        = new HashMap<>();
89
    mNextEffectsID= 0;
90
    mNextClientID = 0;
91
    mNextSystemID = 0;
92
    mNextNodeID   = 0;
93
    mNextQueueID  = 1;
94
    mTaskId       = taskID;
95
    mMax          = new int[EffectType.LENGTH];
96

    
97
    EffectType.reset(mMax);
98
    }
99

    
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101

    
102
  long getTaskId()
103
    {
104
    return mTaskId;
105
    }
106

    
107
///////////////////////////////////////////////////////////////////////////////////////////////////
108

    
109
  static void onPauseCommon()
110
    {
111
    onPauseGeneric(mCommonDoneList,mCommonToDoMap);
112
    }
113

    
114
///////////////////////////////////////////////////////////////////////////////////////////////////
115

    
116
  void onPause()
117
    {
118
    onPauseGeneric(mDoneList,mToDoMap);
119
    }
120

    
121
///////////////////////////////////////////////////////////////////////////////////////////////////
122

    
123
  static void onPauseGeneric(LinkedList<InternalObject> list, HashMap<Long,Job> map)
124
    {
125
    int num = list.size();
126

    
127
    try
128
      {
129
      for (int i=0; i<num; i++)
130
        {
131
        InternalObject object = list.removeFirst();
132
        Job job = new Job(object, InternalObject.JOB_CREATE);
133
        map.put(object.getID(),job);
134
        object.recreate();
135
        }
136
      }
137
    catch( Exception ignored )
138
      {
139
      // something else removed an object in the meantime; ignore
140
      }
141
    }
142

    
143
///////////////////////////////////////////////////////////////////////////////////////////////////
144

    
145
  void toDo()
146
    {
147
    toDoGeneric(mDoneList,mToDoMap);
148
    }
149

    
150
///////////////////////////////////////////////////////////////////////////////////////////////////
151

    
152
  static void toDoCommon()
153
    {
154
    toDoGeneric(mCommonDoneList,mCommonToDoMap);
155
    }
156

    
157
///////////////////////////////////////////////////////////////////////////////////////////////////
158

    
159
  static void toDoGeneric(LinkedList<InternalObject> list, HashMap<Long,Job> map)
160
    {
161
    for(Long key: map.keySet())
162
      {
163
      Job job = map.get(key);
164
      InternalObject object = job.object;
165

    
166
      if( job.action==InternalObject.JOB_CREATE )
167
        {
168
        object.create();
169
        list.add(object);
170
        }
171
      else if( job.action==InternalObject.JOB_DELETE )
172
        {
173
        object.delete();
174
        }
175
      }
176

    
177
    map.clear();
178
    }
179

    
180
///////////////////////////////////////////////////////////////////////////////////////////////////
181

    
182
  static void cleanCommon()
183
    {
184
    mCommonDoneList.clear();
185
    mCommonToDoMap.clear();
186
    mCommonNextClientID = 0;
187
    mCommonNextSystemID = 0;
188
    }
189

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

    
192
  long generateID(int type, int storage)
193
    {
194
    if( storage==InternalObject.STORAGE_PRIVATE )
195
      {
196
      return type==InternalObject.TYPE_SYST ? --mNextSystemID : ++mNextClientID;
197
      }
198
    else
199
      {
200
      return type==InternalObject.TYPE_SYST ? --mCommonNextSystemID : ++mCommonNextClientID;
201
      }
202
    }
203

    
204
///////////////////////////////////////////////////////////////////////////////////////////////////
205

    
206
  void addToDoneList(InternalObject obj, int storage)
207
    {
208
    if( storage==InternalObject.STORAGE_PRIVATE )
209
      {
210
      if( !mDoneList.contains(obj) )
211
        {
212
        mDoneList.add(obj);
213
        }
214
      }
215
    else
216
      {
217
      if( !mCommonDoneList.contains(obj) ) mCommonDoneList.add(obj);
218
      }
219
    }
220

    
221
///////////////////////////////////////////////////////////////////////////////////////////////////
222

    
223
  void removeFromDoneList(InternalObject obj, int storage)
224
    {
225
    if( storage==InternalObject.STORAGE_PRIVATE )
226
      {
227
      mDoneList.remove(obj);
228
      }
229
    else
230
      {
231
      mCommonDoneList.remove(obj);
232
      }
233
    }
234

    
235
///////////////////////////////////////////////////////////////////////////////////////////////////
236

    
237
  void markFor(InternalObject obj, long id, int storage, int jobType)
238
    {
239
    if( storage==InternalObject.STORAGE_PRIVATE )
240
      {
241
      mDoneList.remove(obj);
242
      mToDoMap.put(id, new Job(obj,jobType) );
243
      }
244
    else
245
      {
246
      mCommonDoneList.remove(obj);
247
      mCommonToDoMap.put(id, new Job(obj,jobType) );
248
      }
249
    }
250

    
251
///////////////////////////////////////////////////////////////////////////////////////////////////
252

    
253
  boolean isInitialized()
254
    {
255
    return mInitialized;
256
    }
257

    
258
///////////////////////////////////////////////////////////////////////////////////////////////////
259

    
260
  void setInitialized(boolean init)
261
    {
262
    mInitialized = init;
263
    }
264

    
265
///////////////////////////////////////////////////////////////////////////////////////////////////
266

    
267
  long getNextEffectsID()
268
    {
269
    return ++mNextEffectsID;
270
    }
271

    
272
///////////////////////////////////////////////////////////////////////////////////////////////////
273

    
274
  long getNextEffectID()
275
    {
276
    return mNextEffectID++;
277
    }
278

    
279
///////////////////////////////////////////////////////////////////////////////////////////////////
280

    
281
  int getMax(int index)
282
    {
283
    return mMax[index];
284
    }
285

    
286
///////////////////////////////////////////////////////////////////////////////////////////////////
287

    
288
  boolean setMax(int index, int max)
289
    {
290
    if( !mInitialized || max<mMax[index] )
291
      {
292
      mMax[index] = max;
293
      return true;
294
      }
295

    
296
    return false;
297
    }
298

    
299
///////////////////////////////////////////////////////////////////////////////////////////////////
300

    
301
  public HashMap<ArrayList<Long>,Long> getMap()
302
    {
303
    return mMapID;
304
    }
305

    
306
///////////////////////////////////////////////////////////////////////////////////////////////////
307

    
308
  public long getNextQueueID()
309
    {
310
    mNextQueueID++;
311

    
312
    return mNextQueueID-1;
313
    }
314

    
315
///////////////////////////////////////////////////////////////////////////////////////////////////
316

    
317
  InternalNodeData getMapID(ArrayList<Long> key)
318
    {
319
    return mMapNodeID.get(key);
320
    }
321

    
322
///////////////////////////////////////////////////////////////////////////////////////////////////
323

    
324
  InternalNodeData putNewDataToMap(ArrayList<Long> key)
325
    {
326
    InternalNodeData data = new InternalNodeData(++mNextNodeID,key);
327
    mMapNodeID.put(key,data);
328
    return data;
329
    }
330

    
331
///////////////////////////////////////////////////////////////////////////////////////////////////
332

    
333
  void removeKeyFromMap(ArrayList<Long> key)
334
    {
335
    mMapNodeID.remove(key);
336
    }
337

    
338
///////////////////////////////////////////////////////////////////////////////////////////////////
339

    
340
  ArrayList<InternalMaster.Slave> getSet()
341
    {
342
    return mSlaves;
343
    }
344

    
345
///////////////////////////////////////////////////////////////////////////////////////////////////
346

    
347
  void debugLists(String frameMarker)
348
    {
349
    debugListsGeneric(mDoneList,mToDoMap,frameMarker);
350
    }
351

    
352
///////////////////////////////////////////////////////////////////////////////////////////////////
353

    
354
  static void debugCommonList()
355
    {
356
    debugListsGeneric(mCommonDoneList,mCommonToDoMap,"Common");
357
    }
358

    
359
///////////////////////////////////////////////////////////////////////////////////////////////////
360

    
361
  static void debugListsGeneric(LinkedList<InternalObject> list, HashMap<Long,Job> map,String frameMarker)
362
    {
363
    android.util.Log.e("Object", frameMarker);
364
    android.util.Log.e("Object", "  Done list:");
365

    
366
    for(InternalObject object : list)
367
      {
368
      object.print("  ");
369
      }
370

    
371
    android.util.Log.e("Object", "  ToDo list:");
372

    
373
    Job job;
374

    
375
    for(Long key: map.keySet())
376
      {
377
      job = map.get(key);
378
      job.object.print(job.action==InternalObject.JOB_CREATE ? " create":" delete");
379
      }
380
    }
381

    
382
///////////////////////////////////////////////////////////////////////////////////////////////////
383

    
384
  void debugMap(String frameMarker)
385
    {
386
    android.util.Log.e("Object", frameMarker);
387
    InternalNodeData tmp;
388

    
389
    for(ArrayList<Long> key: mMapNodeID.keySet())
390
      {
391
      tmp = mMapNodeID.get(key);
392
      android.util.Log.e("NodeData", "NodeID: "+tmp.ID+" <-- "+key);
393
      }
394
    }
395
  }
(14-14/16)