Generics and abstracts are mind boggling difficult to deal with, so bear with me while I try my best to explain the problem in a simple manner.
I have the following classes:
public class Community<T extends Person> {
List<T> people;
public void add(T person) {
people.add(person);
}
public List<T> getPeople() {
return people;
}
}
public abstract class Person {}
public class Aussie extends Person {}
Here is the code to instantiate a community of Aussies:
Aussie aus1 = new Aussie();
Aussie aus2 = new Aussie();
Community<Aussie> aussieCommunity = new Community<>();
aussieCommunity.add(aus1);
aussieCommunity.add(aus2);
Now let's take a step further and say that I have multiple communities that I wish to systematically store inside a list as follow:
List<Community<?>> communities;
I hope you're still with me because here is my problem:
I need to write a code that will take the list of community and display each person's details - assuming each person's details will be accessed differently in their own class. Example: Aussie may say "Oi" as hi, American's may say "Hello" as hi.
for (Community<?> community : communities) {
// I don't know what the type of community this is so, I use wildcard:
List<? extends Person> people = community.getPeople();
for (Type person : people) { // How do I specify the type of person eg Aussie/American etc here?
// Do something
}
}
Any suggestion on how I can specify the type of person in the second for loop?
Ok. Here is an small example of how it can be done:
public abstract class Person {
public final String say(String sentance) {
StringTokenizer tokenizer = new StringTokenizer(sentance);
StringBuilder sb = new StringBuilder();
while (tokenizer.hasMoreTokens()) {
String word = tokenizer.nextToken();
String slang = getSlang(word);
sb.append(slang != null ? slang : word);
sb.append(tokenizer.hasMoreTokens() ? " " : "");
}
return sb.toString();
}
private String getSlang(String word) {
return getSlangMap().get(word);
}
protected abstract Map<String, String> getSlangMap();
}
public class Aussi extends Person {
#Override
protected Map<String, String> getSlangMap() {
Map<String, String> slangMap = new HashMap<>();
slangMap.put("hi", "Oi");
slangMap.put("there", "theeer");
return slangMap;
}
}
public class Swede extends Person {
#Override
protected Map<String, String> getSlangMap() {
Map<String, String> slangMap = new HashMap<>();
slangMap.put("hi", "hejsan");
slangMap.put("there", "där");
return slangMap;
}
}
public class CommunityTest {
#Test
public void testSayHiThere() throws Exception {
Aussi au1 = new Aussi();
Aussi au2 = new Aussi();
Community<Aussi> aussiCommunity = new Community<>();
aussiCommunity.add(au1);
aussiCommunity.add(au2);
Swede sw1 = new Swede();
Swede sw2 = new Swede();
Community<Swede> swedeCommunity = new Community<>();
swedeCommunity.add(sw1);
swedeCommunity.add(sw2);
List<Community<? extends Person>> communities = new ArrayList<>();
communities.add(aussiCommunity);
communities.add(swedeCommunity);
for (Community<? extends Person> community : communities) {
for (Person person : community.getPeople()) {
System.out.println(person.say("hi there"));
}
}
}
}
Related
I'm getting back into Java programming, and I have a situation where I'd like to do something like this:
public class FieldAliasTest {
static public class Something {
#Alias("somethingId")
public Long id;
#Alias("id")
public Long somethingId;
}
static public void main(String[]args) {
Something mySomething = new Something();
mySomething.id = 1L;
assert(mySomething.somethingId == 1L);
mySomething.somethingId = 2L;
assert(mySomething.id == 2L);
}
}
Is there anything in the Java standard, Spring Boot, and/or Lombok libraries that would make that possible?
Is it possible at all?
I'm trying to avoid adding any functions like setSomethingId() and setId().
In short, no. Java does not support this.
You can build something like this, noting that you define aliases per instance, not on the class itself. Modify as needed:
public class AliasThingy {
private final Map<String, Object> storage = new HashMap<>();
private final Map<String, String> aliases = new HashMap<>();
public AliasThingy (Map<String, List<String>> aliases) {
aliases.forEach((key, value) -> value.forEach(alias -> this.aliases.put(alias, key)));
}
public void set(String key, Object value) {
storage.put(aliases.getOrDefault(key, key), value);
}
public <T> T get(String key) {
return (T)storage.get(aliases.getOrDefault(key, key));
}
public static void main(String[] args) {
var something = new AliasThingy(Map.of(
"id", List.of("somethingId")
));
something.set("id", 1L);
assert((long)something.get("somethingId") == 1L);
something.set("somethingId", 2L);
assert((long)something.get("id") == 2L);
}
}
You could also use it as a base class, which lets you define the aliases in the class definition, plus an opportunity to avoid raw strings:
public class Something extends AliasThingy {
public interface KEYS {
String ID = "id";
String SOMETHING_ID = "somethingId";
String NAME = "name";
String DESCRIPTION = "description";
String TITLE = "title";
}
public Something() {
super(Map.of(
KEYS.ID, List.of(KEYS.SOMETHING_ID),
KEYS.NAME, List.of(KEYS.DESCRIPTION, KEYS.TITLE)
));
}
public static void main(String[] args) {
Something something = new Something();
something.set(KEYS.ID, 1L);
assert((long)something.get(KEYS.SOMETHING_ID) == 1L);
something.set(KEYS.SOMETHING_ID, 2L);
assert((long)something.get(KEYS.ID) == 2L);
something.set(KEYS.TITLE, "Dr. Strangelove");
assert(something.get(KEYS.NAME).equals("Dr. Strangelove"));
assert(something.get(KEYS.DESCRIPTION).equals("Dr. Strangelove"));
}
}
Edit: I was trying to simplify my problem at hand a little, but turns out, it created more confusion instead. Here's the real deal:
I am working with AWS's Java SDK for DynamoDB. Using the DynamoDBMapper class, I am trying to query DynamoDB to retrieve items from a particular table. I have several objects that map to my DynamoDB tables, and I was hoping to have a generic method that could accept the mapped objects, query the table, and return the item result.
Psudo-code:
#DynamoDBTable(tableName="testTable")
public class DBObject {
private String hashKey;
private String attribute1;
#DynamoDBHashKey(attributeName="hashKey")
public String getHashKey() { return this.hashKey; }
public void setHashKey(String hashKey)
#DynamoDBAttribute(attributeName="attribute1")
public String getAttribute1() { return this.attribute1; }
public void setAttribute1(String attribute1) { this.attribute1 = attribute1; }
}
public class DatabaseRetrieval {
public DatabaseRetrieval()
{
DBObject dbObject = new DBObject();
dbObject.setHashKey = "12345";
DBRetrievalAgent agent = new DBRetrievalAgent;
dbObject = agent.retrieveDBObject(dbObject.getClass(), dbObject);
}
}
public class DBRetrievalAgent {
public Object retrieveDBObject(Class<?> classType, Object dbObject)
{
DynamoDBQueryExpression<classType> temp = new DynamoDBQueryExpression<classType>().withHashKeyValues(dbObject);
return this.dynamoDBMapper.query(classType, temp);
}
}
Use a type witness within your method:
public <T> String getResult(Class<T> type) {
List<T> newList = new ArrayList<>();
//other code
}
Try this
ArrayList<T> newList = new ArrayList<>();
You can specify the type as T for your getResult() to make it generic (i.e., accepts any class) as shown below:
public <T> String getResult(T t) {
String result = "";
List<T> newList = new ArrayList<>();
// perform actions
return result;
}
I have 4 almost same function and would like to refactor them in one. I thought to use reflexy, but I don't understand how to init EnumMap with reflexy.
code of functions like this:
private void loadRealEstateValues() {
realEstateValues = new EnumMap<RealEstateType, String>(RealEstateType.class);
for (RealEstateType r : RealEstateType.values()) {
realEstateValues.put(r, ResUtils.getStringByName(context,
String.format("%s_%s", getParserPrefix(), r.toString().toLowerCase())));
}
}
private void loadPeriodValues() {
periodValues = new EnumMap<PeriodType, String>(PeriodType.class);
for (PeriodType p : PeriodType.values()) {
periodValues.put(p, ResUtils.getStringByName(context,
String.format("%s_%s", getParserPrefix(), p.toString().toLowerCase())));
}
}
ways to put values I found like this, Maybe is a better?
Class<?> c= TestEnum.class;
testEnumMap = new EnumMap<TestEnum, String>(TestEnum.class);
for(Object m : c.getEnumConstants()){
testEnumMap.put((TestEnum) m, "bla bla blah");
}
Use a generic method:
private <T extends Enum<T>> EnumMap<T, String> loadEnumValues(Class<T> enumClass) {
EnumMap<T, String> map = new EnumMap<T, String>(enumClass);
for (T t : enumClass.getEnumConstants()) {
map.put(t, ResUtils.getStringByName(context,
String.format("%s_%s", getParserPrefix(), t.toString().toLowerCase())));
}
return map;
}
I'm obviously missing something here, as this sound basic enough but yet...
I have a collection of objects . I need to use each one of them as parameter in constructor for a new object and return each new object to the caller method, one by one.
But -if I loop over the collection obviously the loop only runs once, and only returns the 1st object.
Edit : Returning the whole collection or some new collection will not work because :
The caller method [not mine to change] runs inside a start() method of a Runnable ThingProvider, which returns a single Thing whenever a request is submitted to it. So, returning List is not possible.
Thanks :)
public List<T> loop(Collection<? extends U> coll) {
List<T> a = new ArrayList<T>();
for (U u : coll){
a.add(new T(u));
}
return a;
}
Return a custom Iterator. Assumming your new objects are of class MyObject and the constructor accepts an Object:
public Iterator<MyObject> myObjectsIterator(final Iterator<? extends Object> it) {
return new Iterator<MyObject>() {
public boolean hasNext() {
return it.hasNext();
}
public MyObject next() {
return new MyObject(it.next());
}
public void remove() {
it.remove();
}
};
}
And you would call it like this:
...
Iterator<MyObject> myIt = myObjectsIterator(myListOfObjects.iterator());
// Now you can pass myIt around as a normal object. It will remember
// which one is the next Object with which to construct a MyObject
// and will generate it on the fly
...
while (myIt.hasNext()) { // is there any MyObject remaining?
MyObject myObj = myIt.next(); // gets the next MyObject
// do something with myObj
}
...
This is a poorly worded question and I think as others have noted, just returning a new list of the objects is fine. But if you really want to process them one at a time while you're looping through it, you can use the command pattern.
public interface Command {
void execute(NewType object);
}
Now in your caller method, you can do the following:
public void doSomething() {
processList(myList, new Command() {
void execute(NewType object) {
// Do whatever you want with this object
}
});
}
And, in the method that will actually go through the list:
public void processList(Iterable<OldType> values, Command command) {
for(OldType v : values) {
NewType newType = new NewType(v);
command.execute(newType);
}
}
In java you can return only once. So if you want to get some informations from your methods either you wrap them into a "Big" Object (here a List) or you give to the method the means to put informations in your parameters.
You could have something like this :
public static void main(String... args){
List<Parameter> parameters = methodToGetParameters();
List<Result> results = generateObjectsFromList(parameters);
for(Result result : results){
handleAResult(result);
}
}
public List<Result> generateObjectsFromList(List<Parameter> parameters){
List<Result> results = new ArrayList<Result>();
for(Parameter parameter : parameters){
results.add(new Result(parameter));
}
return results;
}
Or like this :
public static void main(String... args){
List<Parameter> parameters = methodToGetParameters();
List<Result> results = new ArrayList<Result>();
generateObjectsFromList(parameters, results);
for(Result result : results){
handleAResult(result);
}
}
public void generateObjectsFromList(List<Parameter> parameters, List<Result> results){
for(Parameter parameter : parameters){
results.add(new Result(parameter));
}
}
A third way to do this would be to use fields, but it's not really good to have a lot of fields if they're not really used (or only by one method).
On the same topic :
Java Object Oriented Design Question: Returning multiple objects in java(Updated)
Using a java method to return multiple values?
Return a collection from the method and in the collection implement a custom iterator to transform the input collection to the new collection. The following code shows how to do it using the Google Guava library:
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;
public class Test {
static class Person {
public final String name;
public Person(String name) {
this.name = name;
}
}
public static Collection<Person> peopleFromNames(Collection<String> names) {
return Collections2.transform(names, new Function<String, Person>() {
public Person apply(String name) {
return new Person(name);
}});
}
public static void main(String[] args) {
List<String> names = Arrays.asList("Brian", "Albert", "Roger");
for (Person person : peopleFromNames(names)) {
System.out.println(person.name);
}
}
}
do you mean using of delegates something like below
public class Test {
private static class Person{
private final String name;
Person(String name){
this.name = name;
}
#Override
public String toString() {
return return name;
}
}
private interface Printer {
void print(Object object);
}
public static void main(String[] args) {
final String[] names = {"one", "two", "three"};
final ArrayList<Person> people = construct(names, new Printer() {
#Override
public void print(Object object) {
System.out.println(object.toString());
}
});
}
private static ArrayList<Person> construct(String[] names, Printer printer) {
ArrayList<Person> people = new ArrayList<Person>();
for (String name : names) {
printer.print(new Person(name));
}
return people;
}
}
It's Possible.
Check these Project for Java-yield , yield4Java, infomancers
If you're using this just once in your entire code, You're better off choosing a method from the other answers.
Return a list of the new objects.
I have a class, the outline of which is basically listed below.
import org.apache.commons.math.stat.Frequency;
public class WebUsageLog {
private Collection<LogLine> logLines;
private Collection<Date> dates;
WebUsageLog() {
this.logLines = new ArrayList<LogLine>();
this.dates = new ArrayList<Date>();
}
SortedMap<Double, String> getFrequencyOfVisitedSites() {
SortedMap<Double, String> frequencyMap = new TreeMap<Double, String>(Collections.reverseOrder()); //we reverse order to sort from the highest percentage to the lowest.
Collection<String> domains = new HashSet<String>();
Frequency freq = new Frequency();
for (LogLine line : this.logLines) {
freq.addValue(line.getVisitedDomain());
domains.add(line.getVisitedDomain());
}
for (String domain : domains) {
frequencyMap.put(freq.getPct(domain), domain);
}
return frequencyMap;
}
}
The intention of this application is to allow our Human Resources folks to be able to view Web Usage Logs we send to them. However, I'm sure that over time, I'd like to be able to offer the option to view not only the frequency of visited sites, but also other members of LogLine (things like the frequency of assigned categories, accessed types [text/html, img/jpeg, etc...] filter verdicts, and so on). Ideally, I'd like to avoid writing individual methods for compilation of data for each of those types, and they could each end up looking nearly identical to the getFrequencyOfVisitedSites() method.
So, my question is twofold: first, can you see anywhere where this method should be improved, from a mechanical standpoint? And secondly, how would you make this method more generic, so that it might be able to handle an arbitrary set of data?
This is basically the same thing as Eugene's solution, I just left all the frequency calculation stuff in the original method and use the strategy only for getting the field to work on.
If you don't like enums you could certainly do this with an interface instead.
public class WebUsageLog {
private Collection<LogLine> logLines;
private Collection<Date> dates;
WebUsageLog() {
this.logLines = new ArrayList<LogLine>();
this.dates = new ArrayList<Date>();
}
SortedMap<Double, String> getFrequency(LineProperty property) {
SortedMap<Double, String> frequencyMap = new TreeMap<Double, String>(Collections.reverseOrder()); //we reverse order to sort from the highest percentage to the lowest.
Collection<String> values = new HashSet<String>();
Frequency freq = new Frequency();
for (LogLine line : this.logLines) {
freq.addValue(property.getValue(line));
values.add(property.getValue(line));
}
for (String value : values) {
frequencyMap.put(freq.getPct(value), value);
}
return frequencyMap;
}
public enum LineProperty {
VISITED_DOMAIN {
#Override
public String getValue(LogLine line) {
return line.getVisitedDomain();
}
},
CATEGORY {
#Override
public String getValue(LogLine line) {
return line.getCategory();
}
},
VERDICT {
#Override
public String getValue(LogLine line) {
return line.getVerdict();
}
};
public abstract String getValue(LogLine line);
}
}
Then given an instance of WebUsageLog you could call it like this:
WebUsageLog usageLog = ...
SortedMap<Double, String> visitedSiteFrequency = usageLog.getFrequency(VISITED_DOMAIN);
SortedMap<Double, String> categoryFrequency = usageLog.getFrequency(CATEGORY);
I'd introduce an abstraction like "data processor" for each computation type, so you can just call individual processors for each line:
...
void process(Collection<Processor> processors) {
for (LogLine line : this.logLines) {
for (Processor processor : processors) {
processor.process();
}
}
for (Processor processor : processors) {
processor.complete();
}
}
...
public interface Processor {
public void process(LogLine line);
public void complete();
}
public class FrequencyProcessor implements Processor {
SortedMap<Double, String> frequencyMap = new TreeMap<Double, String>(Collections.reverseOrder()); //we reverse order to sort from the highest percentage to the lowest.
Collection<String> domains = new HashSet<String>();
Frequency freq = new Frequency();
public void process(LogLine line)
String property = getProperty(line);
freq.addValue(property);
domains.add(property);
}
protected String getProperty(LogLine line) {
return line.getVisitedDomain();
}
public void complete()
for (String domain : domains) {
frequencyMap.put(freq.getPct(domain), domain);
}
}
}
You could also change a LogLine API to be more like a Map, i.e. instead of strongly typed line.getVisitedDomain() could use line.get("VisitedDomain"), then you can write a generic FrequencyProcessor for all properties and just pass a property name in its constructor.