Project

General

Profile

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

distorted-objectlib / src / main / java / org / distorted / objectlib / tablebases / ImplementedTablebasesList.java @ 76de8309

1 1725b607 Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
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 c0266cb1 Leszek Koltunski
import org.distorted.objectlib.main.ObjectSignatures;
15 1725b607 Leszek Koltunski
16
import java.lang.reflect.Constructor;
17
import java.lang.reflect.InvocationTargetException;
18
19
///////////////////////////////////////////////////////////////////////////////////////////////////
20
21
public enum ImplementedTablebasesList
22
{
23 db2ed801 Leszek Koltunski
  PDUO     (ObjectSignatures.PDUO_2, TBPyraminxDuo.class),
24
  IVY      (ObjectSignatures.IVY_2 , TBIvyCube.class),
25
  CU_232   (ObjectSignatures.CU_232, TBCuboid232.class),
26
  PYRA_3   (ObjectSignatures.PYRA_3, TBPyraminx.class),
27
  DIAM2    (ObjectSignatures.DIAM_2, TBSkewbDiamond.class),
28
  CUBE2    (ObjectSignatures.CUBE_2, TBCube2.class),
29
  MIRR2    (ObjectSignatures.MIRR_2, TBCube2.class),
30 8b57a8f9 Leszek Koltunski
  JING2    (ObjectSignatures.JING_2, TBJing.class),
31 e9bff328 Leszek Koltunski
  SKEW2    (ObjectSignatures.SKEW_2, TBSkewb.class),
32 1725b607 Leszek Koltunski
  ;
33
34
  public static final int NUM_OBJECTS = values().length;
35
36 c0266cb1 Leszek Koltunski
  private final int mSignature;
37 1725b607 Leszek Koltunski
  private final Class<? extends TablebasesAbstract> mClass;
38
39
  private static final ImplementedTablebasesList[] objects;
40
41
  static
42
    {
43
    objects = new ImplementedTablebasesList[NUM_OBJECTS];
44
    int i=0;
45
46
    for(ImplementedTablebasesList object: ImplementedTablebasesList.values())
47
      {
48
      objects[i++] = object;
49
      }
50
    }
51
52
///////////////////////////////////////////////////////////////////////////////////////////////////
53
54 c0266cb1 Leszek Koltunski
  public static boolean hasTablebase(int signature)
55 1725b607 Leszek Koltunski
    {
56
    for(int i=0; i<NUM_OBJECTS; i++)
57 c0266cb1 Leszek Koltunski
      if( objects[i].mSignature == signature ) return true;
58 1725b607 Leszek Koltunski
59
    return false;
60
    }
61
62
///////////////////////////////////////////////////////////////////////////////////////////////////
63
64 c0266cb1 Leszek Koltunski
  ImplementedTablebasesList(int sig, final Class<? extends TablebasesAbstract> clazz)
65 1725b607 Leszek Koltunski
    {
66 c0266cb1 Leszek Koltunski
    mSignature = sig;
67 1725b607 Leszek Koltunski
    mClass= clazz;
68
    }
69
70
///////////////////////////////////////////////////////////////////////////////////////////////////
71
72 c0266cb1 Leszek Koltunski
  public int getSignature()
73 1725b607 Leszek Koltunski
    {
74 c0266cb1 Leszek Koltunski
    return mSignature;
75 1725b607 Leszek Koltunski
    }
76
77
///////////////////////////////////////////////////////////////////////////////////////////////////
78
79 884b702b Leszek Koltunski
  private static TablebasesAbstract create(Resources res, int signature, boolean packed)
80 1725b607 Leszek Koltunski
    {
81
    Class<? extends TablebasesAbstract> clazz=null;
82
83
    for(int i=0; i<NUM_OBJECTS; i++)
84 c0266cb1 Leszek Koltunski
      if( objects[i].mSignature == signature )
85 1725b607 Leszek Koltunski
        {
86
        clazz = objects[i].mClass;
87
        break;
88
        }
89
90
    if( clazz==null ) return null;
91
92
    try
93
      {
94
      Constructor<?>[] cons = clazz.getConstructors();
95
96
      if( cons.length==2 )
97
        {
98 884b702b Leszek Koltunski
        if( packed )
99
          {
100
          Object[] parameters = new Object[] { res };
101
          return (TablebasesAbstract)cons[1].newInstance(parameters);
102
          }
103
        else
104
          {
105
          return (TablebasesAbstract)cons[0].newInstance();
106
          }
107 1725b607 Leszek Koltunski
        }
108
      else
109
        {
110
        android.util.Log.e("TablebasesList", "ERROR! number of TablebasesAbstract constructors="+cons.length);
111
        }
112
      }
113
    catch(IllegalAccessException iae)
114
      {
115
      android.util.Log.e("TablebasesList", "Illegal Access Exception: "+iae.getMessage());
116
      }
117
    catch(InstantiationException ie)
118
      {
119
      android.util.Log.e("TablebasesList", "Instantiation Exception: "+ie.getMessage());
120
      }
121
    catch(InvocationTargetException ite)
122
      {
123
      android.util.Log.e("TablebasesList", "Invocation Target Exception: "+ite.getMessage());
124
      }
125
126
    return null;
127
    }
128
129
///////////////////////////////////////////////////////////////////////////////////////////////////
130
131 884b702b Leszek Koltunski
  public static TablebasesAbstract createPacked(Resources res, int signature)
132 1725b607 Leszek Koltunski
    {
133 884b702b Leszek Koltunski
    return create(res,signature,true);
134
    }
135 1725b607 Leszek Koltunski
136 884b702b Leszek Koltunski
///////////////////////////////////////////////////////////////////////////////////////////////////
137 1725b607 Leszek Koltunski
138 884b702b Leszek Koltunski
  public static TablebasesAbstract createUnpacked(int signature)
139
    {
140
    return create(null,signature,false);
141 1725b607 Leszek Koltunski
    }
142
}