Project

General

Profile

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

library / src / main / java / org / distorted / library / DistortedGridFactory.java @ 665e2c45

1 0729bc41 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2016 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;
21
22
import java.util.HashMap;
23
/**
24
 * HashMap of all DistortedObjectGrids ever created. The point: we can share the Grids among
25
 * DistortedObjects that are of the same shape.
26
 */
27
///////////////////////////////////////////////////////////////////////////////////////////////////
28
29
final class DistortedGridFactory
30
  {
31
  private static HashMap<String,DistortedObjectGrid> mGrids = new HashMap<>();
32
33
///////////////////////////////////////////////////////////////////////////////////////////////////
34
// DistortedCubesGrid
35
36
  static synchronized DistortedObjectGrid getGrid(int cols, String desc, boolean frontOnly)
37
    {
38 665e2c45 Leszek Koltunski
    String d = "1_"+cols+"_"+desc+"_"+(frontOnly?"1":"0");
39 0729bc41 Leszek Koltunski
    Object o = mGrids.get(d);
40
41 62f7a90a Leszek Koltunski
    if( o!=null )
42
      {
43
      return (DistortedObjectGrid)o;
44
      }
45
    else
46
      {
47
      DistortedObjectGrid grid = new DistortedCubesGrid(cols,desc,frontOnly);
48
      mGrids.put(d,grid);
49
      return grid;
50
      }
51 0729bc41 Leszek Koltunski
    }
52
53 665e2c45 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
54
// DistortedCubesGrid (full)
55
56
  static synchronized DistortedObjectGrid getGrid(int cols, int rows, boolean frontOnly)
57
    {
58
    String d = "2_"+cols+"_"+rows+"_"+(frontOnly?"1":"0");
59
    Object o = mGrids.get(d);
60
61
    if( o!=null )
62
      {
63
      return (DistortedObjectGrid)o;
64
      }
65
    else
66
      {
67
      DistortedObjectGrid grid = new DistortedCubesGrid(cols,rows,frontOnly);
68
      mGrids.put(d,grid);
69
      return grid;
70
      }
71
    }
72
73 0729bc41 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
74
// DistortedBitmapGrid
75
76
  static synchronized DistortedObjectGrid getGrid(int cols, int rows)
77
    {
78
    String d = cols+"+"+rows;
79
    Object o = mGrids.get(d);
80
81 62f7a90a Leszek Koltunski
    if( o!=null )
82
      {
83
      return (DistortedObjectGrid)o;
84
      }
85
    else
86
      {
87
      DistortedObjectGrid grid = new DistortedBitmapGrid(cols,rows);
88
      mGrids.put(d,grid);
89
      return grid;
90
      }
91 0729bc41 Leszek Koltunski
    }
92
93
///////////////////////////////////////////////////////////////////////////////////////////////////
94
95
  static synchronized void release()
96
    {
97
    mGrids.clear();
98
    }
99
100
///////////////////////////////////////////////////////////////////////////////////////////////////
101
  }