Project

General

Profile

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

library / src / main / java / org / distorted / library / main / InternalStackFrame.java @ 3543a3cf

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

    
74
  InternalStackFrame(long taskID)
75
    {
76
    mDoneList     = new LinkedList<>();
77
    mToDoMap      = new HashMap<>();
78
    mMapNodeID    = new HashMap<>();
79
    mNextEffectsID= 0;
80
    mNextClientID = 0;
81
    mNextSystemID = 0;
82
    mNextNodeID   = 0;
83
    mTaskId       = taskID;
84
    mMax          = new int[EffectType.LENGTH];
85

    
86
    EffectType.reset(mMax);
87
    }
88

    
89
///////////////////////////////////////////////////////////////////////////////////////////////////
90

    
91
  long getTaskId()
92
    {
93
    return mTaskId;
94
    }
95

    
96
///////////////////////////////////////////////////////////////////////////////////////////////////
97

    
98
  static void onPauseCommon()
99
    {
100
    onPauseGeneric(mCommonDoneList,mCommonToDoMap);
101
    }
102

    
103
///////////////////////////////////////////////////////////////////////////////////////////////////
104

    
105
  void onPause()
106
    {
107
    onPauseGeneric(mDoneList,mToDoMap);
108
    }
109

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

    
112
  static void onPauseGeneric(LinkedList<InternalObject> list, HashMap<Long,Job> map)
113
    {
114
    int num = list.size();
115

    
116
    try
117
      {
118
      for (int i=0; i<num; i++)
119
        {
120
        InternalObject object = list.removeFirst();
121
        Job job = new Job(object, InternalObject.JOB_CREATE);
122
        map.put(object.getID(),job);
123
        object.recreate();
124
        }
125
      }
126
    catch( Exception ignored )
127
      {
128
      // something else removed an object in the meantime; ignore
129
      }
130
    }
131

    
132
///////////////////////////////////////////////////////////////////////////////////////////////////
133

    
134
  void toDo()
135
    {
136
    toDoGeneric(mDoneList,mToDoMap);
137
    }
138

    
139
///////////////////////////////////////////////////////////////////////////////////////////////////
140

    
141
  static void toDoCommon()
142
    {
143
    toDoGeneric(mCommonDoneList,mCommonToDoMap);
144
    }
145

    
146
///////////////////////////////////////////////////////////////////////////////////////////////////
147

    
148
  static void toDoGeneric(LinkedList<InternalObject> list, HashMap<Long,Job> map)
149
    {
150
    for(Long key: map.keySet())
151
      {
152
      Job job = map.get(key);
153
      InternalObject object = job.object;
154

    
155
      if( job.action==InternalObject.JOB_CREATE )
156
        {
157
        object.create();
158
        list.add(object);
159
        }
160
      else if( job.action==InternalObject.JOB_DELETE )
161
        {
162
        object.delete();
163
        }
164
      }
165

    
166
    map.clear();
167
    }
168

    
169
///////////////////////////////////////////////////////////////////////////////////////////////////
170

    
171
  static void cleanCommon()
172
    {
173
    mCommonDoneList.clear();
174
    mCommonToDoMap.clear();
175
    mCommonNextClientID = 0;
176
    mCommonNextSystemID = 0;
177
    }
178

    
179
///////////////////////////////////////////////////////////////////////////////////////////////////
180

    
181
  long generateID(int type, int storage)
182
    {
183
    if( storage==InternalObject.STORAGE_PRIVATE )
184
      {
185
      return type==InternalObject.TYPE_SYST ? --mNextSystemID : ++mNextClientID;
186
      }
187
    else
188
      {
189
      return type==InternalObject.TYPE_SYST ? --mCommonNextSystemID : ++mCommonNextClientID;
190
      }
191
    }
192

    
193
///////////////////////////////////////////////////////////////////////////////////////////////////
194

    
195
  void addToDoneList(InternalObject obj, int storage)
196
    {
197
    if( storage==InternalObject.STORAGE_PRIVATE )
198
      {
199
      if( !mDoneList.contains(obj) )
200
        {
201
        mDoneList.add(obj);
202
        }
203
      }
204
    else
205
      {
206
      if( !mCommonDoneList.contains(obj) ) mCommonDoneList.add(obj);
207
      }
208
    }
209

    
210
///////////////////////////////////////////////////////////////////////////////////////////////////
211

    
212
  void removeFromDoneList(InternalObject obj, int storage)
213
    {
214
    if( storage==InternalObject.STORAGE_PRIVATE )
215
      {
216
      mDoneList.remove(obj);
217
      }
218
    else
219
      {
220
      mCommonDoneList.remove(obj);
221
      }
222
    }
223

    
224
///////////////////////////////////////////////////////////////////////////////////////////////////
225

    
226
  void markFor(InternalObject obj, long id, int storage, int jobType)
227
    {
228
    if( storage==InternalObject.STORAGE_PRIVATE )
229
      {
230
      mDoneList.remove(obj);
231
      mToDoMap.put(id, new Job(obj,jobType) );
232
      }
233
    else
234
      {
235
      mCommonDoneList.remove(obj);
236
      mCommonToDoMap.put(id, new Job(obj,jobType) );
237
      }
238
    }
239

    
240
///////////////////////////////////////////////////////////////////////////////////////////////////
241

    
242
  boolean isInitialized()
243
    {
244
    return mInitialized;
245
    }
246

    
247
///////////////////////////////////////////////////////////////////////////////////////////////////
248

    
249
  void setInitialized(boolean init)
250
    {
251
    mInitialized = init;
252
    }
253

    
254
///////////////////////////////////////////////////////////////////////////////////////////////////
255

    
256
  long getNextEffectsID()
257
    {
258
    return ++mNextEffectsID;
259
    }
260

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

    
263
  long getNextEffectID()
264
    {
265
    return mNextEffectID++;
266
    }
267

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

    
270
  int getMax(int index)
271
    {
272
    return mMax[index];
273
    }
274

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

    
277
  boolean setMax(int index, int max)
278
    {
279
    if( !mInitialized || max<mMax[index] )
280
      {
281
      mMax[index] = max;
282
      return true;
283
      }
284

    
285
    return false;
286
    }
287

    
288
///////////////////////////////////////////////////////////////////////////////////////////////////
289

    
290
  InternalNodeData getMapID(ArrayList<Long> key)
291
    {
292
    return mMapNodeID.get(key);
293
    }
294

    
295
///////////////////////////////////////////////////////////////////////////////////////////////////
296

    
297
  InternalNodeData putNewDataToMap(ArrayList<Long> key)
298
    {
299
    InternalNodeData data = new InternalNodeData(++mNextNodeID,key);
300
    mMapNodeID.put(key,data);
301
    return data;
302
    }
303

    
304
///////////////////////////////////////////////////////////////////////////////////////////////////
305

    
306
  void removeKeyFromMap(ArrayList<Long> key)
307
    {
308
    mMapNodeID.remove(key);
309
    }
310

    
311
///////////////////////////////////////////////////////////////////////////////////////////////////
312

    
313
  void debugLists(String frameMarker)
314
    {
315
    debugListsGeneric(mDoneList,mToDoMap,frameMarker);
316
    }
317

    
318
///////////////////////////////////////////////////////////////////////////////////////////////////
319

    
320
  static void debugCommonList()
321
    {
322
    debugListsGeneric(mCommonDoneList,mCommonToDoMap,"Common");
323
    }
324

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

    
327
  static void debugListsGeneric(LinkedList<InternalObject> list, HashMap<Long,Job> map,String frameMarker)
328
    {
329
    android.util.Log.e("Object", frameMarker);
330
    android.util.Log.e("Object", "  Done list:");
331

    
332
    for(InternalObject object : list)
333
      {
334
      object.print("  ");
335
      }
336

    
337
    android.util.Log.e("Object", "  ToDo list:");
338

    
339
    Job job;
340

    
341
    for(Long key: map.keySet())
342
      {
343
      job = map.get(key);
344
      job.object.print(job.action==InternalObject.JOB_CREATE ? " create":" delete");
345
      }
346
    }
347

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

    
350
  void debugMap(String frameMarker)
351
    {
352
    android.util.Log.e("Object", frameMarker);
353
    InternalNodeData tmp;
354

    
355
    for(ArrayList<Long> key: mMapNodeID.keySet())
356
      {
357
      tmp = mMapNodeID.get(key);
358
      android.util.Log.e("NodeData", "NodeID: "+tmp.ID+" <-- "+key);
359
      }
360
    }
361
  }
(14-14/16)