1

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;
    }
}
Community
  • 1
  • 1
Iszlai Lehel
  • 233
  • 3
  • 11
  • 1
    it's unclear what you're trying to do here, or how it could possibly work correctly / do anything useful. – Brian Roach Feb 03 '14 at 23:21
  • @BrianRoach Well the ideea would be that you read in large csv file,each line is a task (an array of number) ,those tasks are put in a queue , when someone start processing that task it because in progress , but in the meanwhile other people could also get tasks of the queue. – Iszlai Lehel Feb 03 '14 at 23:45
  • I meant the what looks like trying to make a singleton bit that can somehow change types. How do you suppose calling `getInstance(Integer.class)` is going to work if you've already created and are trying to return a `Store` ? – Brian Roach Feb 03 '14 at 23:52
  • You are right , i didn't think it through i was only focusing on the fact that all clients should be using the same store.And I'm still fuzzy about how to achieve that.But thank you for taking your time to answer @BrianRoach – Iszlai Lehel Feb 03 '14 at 23:56

2 Answers2

6

It's not clear why you're not just writing

public static <T> Store<T> getInstance() {
  return new Store<T>();
}
Louis Wasserman
  • 191,574
  • 25
  • 345
  • 413
  • Sorry for my ignorance but i just don't know ,if I would write it that way how would i give the type parameter ? Like Store s=Store.getInstance() ? – Iszlai Lehel Feb 03 '14 at 23:29
  • 1
    I *think* the OP is trying to make it a singleton. That's the only thing I can come up with. – Brian Roach Feb 03 '14 at 23:30
  • And yes the original ideea would have been ,to make it singleton – Iszlai Lehel Feb 03 '14 at 23:31
  • 1
    @IszlaiLehel: It'd automatically be inferred for you, in this version. If you needed to make it explicit, you could write `Store.getInstance()`. – Louis Wasserman Feb 03 '14 at 23:31
  • Thanks you saved be, i didn't know exactly how type inferece works. – Iszlai Lehel Feb 03 '14 at 23:36
  • @LouisWasserman on a side note ,besides that does the code seem ok?I'm struggling to do my best – Iszlai Lehel Feb 03 '14 at 23:38
  • 1
    If you run into an issue `non-static type variable T cannot be referenced from a static context` which you should, type parameterize the `getInstance` method, @LouisWasserman has shown you the way, but I do not think it is adequate to make the compiler happy – Bhaskar Feb 03 '14 at 23:43
  • @Bhaskar thanks to what you said and reading this I finally understud type inference and generic methos .Thank you really made my day.:) – Iszlai Lehel Feb 04 '14 at 09:43
  • @IszlaiLehel Very nice happy to help, but you did it mostly on your own. – Bhaskar Feb 05 '14 at 01:02
1

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;
}

}

Iszlai Lehel
  • 233
  • 3
  • 11