Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / tablebases / ImplementedTablebasesList.java @ 786cb5f5

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
  IVY_CUBE      (ObjectType.IVY_2 , TablebasesIvyCube.class),
25
  ;
26

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

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

    
32
  private static final ImplementedTablebasesList[] objects;
33

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

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

    
45
///////////////////////////////////////////////////////////////////////////////////////////////////
46

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

    
52
    return false;
53
    }
54

    
55
///////////////////////////////////////////////////////////////////////////////////////////////////
56

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

    
63
///////////////////////////////////////////////////////////////////////////////////////////////////
64

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

    
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71

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

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

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

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

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

    
112
    return null;
113
    }
114

    
115
///////////////////////////////////////////////////////////////////////////////////////////////////
116

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

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

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

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

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

    
156
    return null;
157
    }
158
}
(1-1/5)