0

I need to create a series of transformer classes that transform an object into an other one.

They are of the form

class Transformer1 {
    public static AnotherClass transform(MyClass source) {
        return AnotherClass.newBuilder()
                .setName(source.getName())
                // etc
                .build();
    }
}

I have about 15 of them.

These transformers for now assume that the parameter is not null. Otherwise I would get an NullPointerException.

I would like to add a null check, ie:

class Transformer1 {
    public static AnotherClass transform(MyClass source) {
        if(source == null)
            return null;
        return AnotherClass.newBuilder()
                .setName(source.getName())
                // etc
                .build();
    }
}

but I don't want to write this null check on every transformer class. I was thinking of factoring it in a super class or interface.

abstract class BaseTransformer {

    public static AnotherClass transform(MyClass source) {
        return source == null ? null : transformNonNull(source);
    }

    public abstract AnotherClass transformNonNull(MyClass source);
}

and all my subclasses could implement the transformNonNull method.

Now that doesn't work because we are dealing with static methods and transformNonNull would need to be static. But it can't be static and abstract.

I could make my transformers non static, but really they have no state and are more like utilities, so I would like to keep them static.

I already encountered this issue in the past and didn't really come up with a solution. Waht would be the best approach here?

EDIT:

Object A has fields a1, a2, ... Ax and I want to transform it into an object B with fields b1, b2, ... bx

My transformers are transformers for these fields into one another. In my case, a2 could be null, and that would be perfectly acceptable. I don't want to through an exception if a2 is null and I can't build b2 from it. In this case I want to set b2 to null also

Naman
  • 27,789
  • 26
  • 218
  • 353
Bentaye
  • 9,403
  • 5
  • 32
  • 45
  • 1
    Throwing an exception when the argument is `null`, is a reasonable behavior. Returning `null` instead, only means making it someone else’s problem, at a point where it might be hard to impossible, to find out where the values started to be `null`. So “the best approach” is not to make this questionable change at all. – Holger Jun 25 '20 at 11:13
  • @Holger I understand that in terms of the transformer, it would be better to assume it is never null, but I need to do that check somewhere because null is acceptable in my case and should be "transformed" to null. See my edit. So I am just moving the check to the call, saying, if not null then transform, otherwise set null – Bentaye Jun 25 '20 at 11:52
  • Why are these transformer methods static? Why not have an `interface Transformer{ To transform(From input)}`. Then you can have them inherit super-class methods or have a static helper method to do your null-check before dispatching to a transformer. How do you call your 15 unrelated static methods now? A huge chain of if/else ? – Thilo Jun 25 '20 at 12:08
  • "I could make my transformers non static, but really they have no state and are more like utilities, so I would like to keep them static." State is not the only reason to use objects. Another is dynamic dispatch, code-reuse through inheritance, de-coupling of interface and implementation. – Thilo Jun 25 '20 at 12:10
  • ok guys, I think I will make them non static, and have a super class, it will make things lot easier. I am using Spring so making them singletons is easy. Thanks all for the input. – Bentaye Jun 25 '20 at 13:14

1 Answers1

0

You can use AspectJ to insert the null check into the relevant methods.

Simon G.
  • 6,587
  • 25
  • 30