What's wrong with this Java HashMap declaration? Easy points
private HashMap<String, IMapper> mapper = new HashMap<String, IMapper>();
Seems so innocent yet Eclipse is complaining about the semi-colon, of all things.
Eclipse error: Syntax error on token ";", invalid AssignmentOperator
Thanks
The entire class:
package persistence;
import java.util.UUID;
import java.util.HashMap;
import domain.Task;
public class PersistanceFacade {
private static PersistanceFacade uniqueInstance = null;
private HashMap<String, IMapper> mapper = new HashMap<String, IMapper>();
(SingleTaskRDBMapper) mapper.put("SingleTask", new SingleTaskRDBMapper());
public PersistanceFacade() {};
public static synchronized PersistanceFacade getUniqueInstance() {
if (uniqueInstanc开发者_JS百科e == null) {
uniqueInstance = new PersistanceFacade();
return uniqueInstance;
}
else return uniqueInstance;
}
@SuppressWarnings("unchecked")
public Object get(UUID oid, Class type) {
Object mappers;
IMapper mapper = ((HashMap<String, IMapper>) mappers).get(type);
}
}
The line
(SingleTaskRDBMapper) mapper.put("SingleTask", new SingleTaskRDBMapper());
is out of place. You should put that in the constructor:
public PersistanceFacade() {
mapper.put("SingleTask", new SingleTaskRDBMapper());
}
Also notice I removed the extraneous semicolon after the closing brace of the constructor. I'm also not sure what is going on in the get method. You declare the object mappers
but never initialize it. That will certainly cause an error.
Not sure what IMapper is, some interface I assume, but I just dropped this into Eclipse and it doesn't give me any errors:
private interface IMapper {}
private HashMap<String, IMapper> mapper = new HashMap<String, IMapper>();
Perhaps you could post more of your code?
This code works perfectly :), inspired by help from laz
package persistence;
import java.util.UUID;
import java.util.HashMap;
public class PersistanceFacade {
private static PersistanceFacade uniqueInstance = null;
private HashMap<String, IMapper> mappers = new HashMap<String, IMapper>();
public PersistanceFacade() {
mappers.put("SingleTask", new SingleTaskRDBMapper());
}
public static synchronized PersistanceFacade getUniqueInstance() {
if (uniqueInstance == null) {
uniqueInstance = new PersistanceFacade();
return uniqueInstance;
}
else return uniqueInstance;
}
@SuppressWarnings("unchecked")
public Object get(UUID oid, Class type) {
IMapper mapper = (IMapper) mappers.get(type);
return mapper.get(oid);
}
}
精彩评论