It's not clear why you're not just writing
public static <T> Store<T> getInstance() {
return new Store<T>();
}
質問
I'm having trouble with the getInstance
method, any way I try I get:
Cannot make a static reference to the non-static type T.
How could I make this work? Any code review would be a great help as well. I've checked several related posts but this seems like a peculiar case.
My code:
public class Data<Template>
{
private Template[] data;
private int id;
public Data(Template[] data)
{
this.data = data;
id = Arrays.hashCode(data);
log();
}
public Template[] getData()
{
return data;
}
public Integer getId()
{
return id;
}
private void log()
{
System.out.println(this.toString());
}
@Override
public String toString()
{
StringBuilder tString = new StringBuilder();
tString.append(id + ":");
for (int i = 0; i < data.length; i++)
{
tString.append(data[i] + " ");
}
return tString.toString();
}
}
public class Store<T>
{
private Queue<Data<T>> queue;
private ConcurrentHashMap<Integer, Data<T>> inProgress;
private List<T> results;
private static Store instance = null;
private Store()
{
queue = new ConcurrentLinkedQueue<Data<T>>();
inProgress = new ConcurrentHashMap<Integer, Data<T>>(16, 0.9f, 1);
results = new ArrayList<>();
}
public void addToStore(Data<T> elment)
{
queue.offer(elment);
}
public Data<T> getNextTask()
{
Data<T> element = queue.poll();
inProgress.put(element.getId(), element);
return element;
}
public void taskFinishedSuccessfully(Integer id, T result)
{
inProgress.remove(id);
results.add(result);
}
public List<T> getResults()
{
return results;
}
public static Store getInstance(Class<T> type)
{
if (instance == null)
{
if (type instanceof Integer)
{
instance = new Store<Integer>();
}
if (type instanceof Float)
{
instance = new Store<Float>();
}
if (type instanceof Double)
{
instance = new Store<Double>();
}
}
return instance;
}
}
解決
It's not clear why you're not just writing
public static <T> Store<T> getInstance() {
return new Store<T>();
}
他のヒント
So this is what i ended up doing:
public class Store<T> {
private Queue<Data<T>> queue;
private ConcurrentHashMap<Integer, Data<T>> inProgress;
private List<T> results;
private static Store<Double> doubleInstance = null;
private static Store<Integer> integerInstance = null;
private static Store<Float> floatInstance = null;
private Store() {
queue = new ConcurrentLinkedQueue<Data<T>>();
inProgress = new ConcurrentHashMap<Integer, Data<T>>(16, 0.9f, 1);
results = new ArrayList<>();
}
public void addToStore(Data<T> elment) {
queue.offer(elment);
}
public Data<T> getNextTask() {
Data<T> element = queue.poll();
inProgress.put(element.getId(), element);
return element;
}
public void taskFinishedSuccessfully(Integer id, T result) {
inProgress.remove(id);
results.add(result);
}
public List<T> getResults() {
return results;
}
private static <T> Store<T> getInstance() {
return new Store<T>();
}
public static Store<Double> getDoubleInstance() {
if (doubleInstance == null) {
doubleInstance = Store.<Double> getInstance();
}
return doubleInstance;
}
public static Store<Integer> getIntegerInstance() {
if (integerInstance == null) {
integerInstance = Store.<Integer> getInstance();
}
return integerInstance;
}
public static Store<Float> getFloatInstance() {
if (floatInstance == null) {
floatInstance = Store.<Float> getInstance();
}
return floatInstance;
}
}