Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / tablebases / ImplementedTablebasesList.java @ 1725b607

1
///////////////////////////////////////////////////////////////////////////////////////////////////
2
// Copyright 2023 Leszek Koltunski                                                               //
3
//                                                                                               //
4
// This file is part of Magic Cube.                                                              //
5
//                                                                                               //
6
// Magic Cube is proprietary software licensed under an EULA which you should have received      //
7
// along with the code. If not, check https://distorted.org/magic/License-Magic-Cube.html        //
8
///////////////////////////////////////////////////////////////////////////////////////////////////
9

    
10
package org.distorted.objectlib.tablebases;
11

    
12
import android.content.res.Resources;
13

    
14
import org.distorted.objectlib.main.ObjectType;
15

    
16
import java.lang.reflect.Constructor;
17
import java.lang.reflect.InvocationTargetException;
18

    
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20

    
21
public enum ImplementedTablebasesList
22
{
23
  PYRAMINX_DUO  (ObjectType.PDUO_2, TablebasesPyraminxDuo.class),
24
  ;
25

    
26
  public static final int NUM_OBJECTS = values().length;
27

    
28
  private final ObjectType mType;
29
  private final Class<? extends TablebasesAbstract> mClass;
30

    
31
  private static final ImplementedTablebasesList[] objects;
32

    
33
  static
34
    {
35
    objects = new ImplementedTablebasesList[NUM_OBJECTS];
36
    int i=0;
37

    
38
    for(ImplementedTablebasesList object: ImplementedTablebasesList.values())
39
      {
40
      objects[i++] = object;
41
      }
42
    }
43

    
44
///////////////////////////////////////////////////////////////////////////////////////////////////
45

    
46
  public static boolean hasTablebase(ObjectType type)
47
    {
48
    for(int i=0; i<NUM_OBJECTS; i++)
49
      if( objects[i].mType == type ) return true;
50

    
51
    return false;
52
    }
53

    
54
///////////////////////////////////////////////////////////////////////////////////////////////////
55

    
56
  ImplementedTablebasesList(ObjectType type, final Class<? extends TablebasesAbstract> clazz)
57
    {
58
    mType = type;
59
    mClass= clazz;
60
    }
61

    
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63

    
64
  public ObjectType getType()
65
    {
66
    return mType;
67
    }
68

    
69
///////////////////////////////////////////////////////////////////////////////////////////////////
70

    
71
  public static TablebasesAbstract createPacked(Resources res, ObjectType type)
72
    {
73
    Class<? extends TablebasesAbstract> clazz=null;
74

    
75
    for(int i=0; i<NUM_OBJECTS; i++)
76
      if( objects[i].mType == type )
77
        {
78
        clazz = objects[i].mClass;
79
        break;
80
        }
81

    
82
    if( clazz==null ) return null;
83

    
84
    try
85
      {
86
      Constructor<?>[] cons = clazz.getConstructors();
87

    
88
      if( cons.length==2 )
89
        {
90
        Object[] parameters = new Object[] { res };
91
        return (TablebasesAbstract)cons[1].newInstance(parameters);
92
        }
93
      else
94
        {
95
        android.util.Log.e("TablebasesList", "ERROR! number of TablebasesAbstract constructors="+cons.length);
96
        }
97
      }
98
    catch(IllegalAccessException iae)
99
      {
100
      android.util.Log.e("TablebasesList", "Illegal Access Exception: "+iae.getMessage());
101
      }
102
    catch(InstantiationException ie)
103
      {
104
      android.util.Log.e("TablebasesList", "Instantiation Exception: "+ie.getMessage());
105
      }
106
    catch(InvocationTargetException ite)
107
      {
108
      android.util.Log.e("TablebasesList", "Invocation Target Exception: "+ite.getMessage());
109
      }
110

    
111
    return null;
112
    }
113

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

    
116
  public static TablebasesAbstract createUnpacked(ObjectType type)
117
    {
118
    Class<? extends TablebasesAbstract> clazz=null;
119

    
120
    for(int i=0; i<NUM_OBJECTS; i++)
121
      if( objects[i].mType == type )
122
        {
123
        clazz = objects[i].mClass;
124
        break;
125
        }
126

    
127
    if( clazz==null ) return null;
128

    
129
    try
130
      {
131
      Constructor<?>[] cons = clazz.getConstructors();
132

    
133
      if( cons.length==2 )
134
        {
135
        return (TablebasesAbstract)cons[0].newInstance();
136
        }
137
      else
138
        {
139
        android.util.Log.e("TablebasesList", "ERROR! number of TablebasesAbstract constructors="+cons.length);
140
        }
141
      }
142
    catch(IllegalAccessException iae)
143
      {
144
      android.util.Log.e("TablebasesList", "Illegal Access Exception: "+iae.getMessage());
145
      }
146
    catch(InstantiationException ie)
147
      {
148
      android.util.Log.e("TablebasesList", "Instantiation Exception: "+ie.getMessage());
149
      }
150
    catch(InvocationTargetException ite)
151
      {
152
      android.util.Log.e("TablebasesList", "Invocation Target Exception: "+ite.getMessage());
153
      }
154

    
155
    return null;
156
    }
157
}
(1-1/4)