-1

I would like to write generic code for iteration like below:

  public ClassA assemlbyClassA(ClassC c) {
    Set<ClassB> classBElements = c.getClassBElements();
    ClassA result = new ClassA();
    if (classBElements != null && classBElements.size() > 0) {            
        for (ClassB classB : classBelements) {                
            ClassD classD = new ClassD();
            classD.setMethod1(classB.callMethod1());                    
            classD.setMethod2(classB.callMethod2());
            result.add(classD);
        }
    }
    return result;
}

Classes A,B,C,D are not realated to each other. I'm wondering is it possible and whether the simple solution exists?

BlueLettuce16
  • 1,783
  • 3
  • 17
  • 29
  • What is this code supposed to do? –  Feb 04 '14 at 12:17
  • How come you add `ClassD` to the `Set` ? Do they have a relationship ? – Konstantin Yovkov Feb 04 '14 at 12:21
  • The code looks like it'd run, but that depends what the signature of `ClassA.add(?)` is. – Nick Holt Feb 04 '14 at 12:28
  • My description isn't clear enough. Each of classes: ClassA,ClassB,ClassC and ClassD can be replaced by different class. ClassA is always a wrapper for collection of objects. So basically I would like to have code which iterates a collection of elements and converts list of instances of one class into list/set of instances of another unrelated class by copying specified parameters. – BlueLettuce16 Feb 04 '14 at 13:23

1 Answers1

0

Is it possible: kind of! 'new GenericClass()' is not possible, so has to be replaced.

(Or, see Create instance of generic type in Java?.)

"The simple solution exists": well, that's a matter of what 'simple' means to you.

Here's one way, you can copy and paste it in one piece:

interface StackoverflowAnswer {
    public interface ClassABase<ClassD> {
        void add(ClassD value);
    }
    public interface ClassBBase<X1, X2> {
        X1 callMethod1();
        X2 callMethod2();
    }
    public interface ClassCBase<ClassB> {
        Set<ClassB> getClassBElements();
    }
    public interface ClassDBase<X1, X2> {
        void setMethod1(X1 value);
        void setMethod2(X2 value);
    }

    public static abstract class Z<X1, X2, 
            ClassA extends ClassABase<ClassD>, 
            ClassB extends ClassBBase<X1, X2>, 
            ClassC extends ClassCBase<ClassB>, 
            ClassD extends ClassDBase<X1, X2>> {

        protected abstract ClassA newClassA();

        protected abstract ClassD newClassD();

        public ClassA assemlbyClassA(ClassC c) {
            Set<ClassB> classBElements = c.getClassBElements();
            ClassA result = newClassA();
            if (classBElements != null && classBElements.size() > 0) {
                for (ClassB classB : classBElements) {
                    ClassD classD = newClassD();
                    classD.setMethod1(classB.callMethod1());
                    classD.setMethod2(classB.callMethod2());
                    result.add(classD);
                }
            }
            return result;
        }
    }
}
Community
  • 1
  • 1
Jonas N
  • 1,537
  • 2
  • 19
  • 37