0

I have a function that parse an object. But this function is required in two services and the parameter have same class name, but different package name. What i need is to avoid duplicated code.

Suppose the function is:

    private HashMap<String, Integer> getPagination(PagingRequestType pagingRequestType) {
        int pageSize = 200;
        int pageNumber = 1;
        if(pagingRequestType != null) {
            if (pagingRequestType.getNumberOfRecordsPerPage() != 0) {
                pageSize = pagingRequestType.getNumberOfRecordsPerPage();
            }
            if (pagingRequestType.getStartAtRecordNumber() != 0) {
                pageNumber = pagingRequestType.getStartAtRecordNumber();
            }
        }
        HashMap<String, Integer> result = new HashMap<>();
        result.put("pageNumber", pageNumber);
        result.put("pageSize", pageSize);
        return result;
    }

Possible function calls:

- getPagination(new Abc.PagingRequestType());
- getPagination(new Xyz.PagingRequestType());

PagingRequestType is an auto-generated class in two different packages. The function needs to be implemented once and used in both services.

Thanks.

8
  • 1
    Wait, so do you have two different classes called PagingRequestType? That just screams to be fixed, especially if the two implementations are used the same. Commented Mar 31, 2017 at 7:49
  • 1
    The obvious answer is not to auto-generate PagingRequestType in two places. Commented Mar 31, 2017 at 7:49
  • 2
    Or if that is not avoidable have both PagingRequestType classes implement a common interface that can then be used in this method. Commented Mar 31, 2017 at 7:52
  • Yes, this is not because the classes have the same name, that you can do some generic stuff on it. Commented Mar 31, 2017 at 7:53
  • There are two different WSDL files to support legacy system. Commented Mar 31, 2017 at 7:54

4 Answers 4

1

If you can modify your PagingRequestType classes, it will be a good idea to use a common interface:

class Abc.PagingRequestType implements PagingRequestType
class Xyz.PagingRequestType implements PagingRequestType

interface PagingRequestType {
    getNumberOfRecordsPerPage();
    getStartAtRecordNumber();
}
Sign up to request clarification or add additional context in comments.

Comments

1

The obvious answer is not to auto-generate PagingRequestType in two places.

If you can't do this, you need the two classes to implement a common interface, through which the requisite fields (getNumberOfRecordsPerPage and getStartAtRecordNumber) are available.

If you can't change the classes, you can create an interface with these fields:

interface YourInterface {
  int getNumberOfRecordsPerPage();
  int getStartAtRecordNumber();
}

and implement for the two PagingRequestTypes:

class AbcYourInterface implements YourInterface {
  final Abc.PagingRequestType delegate;  // Set in constructor.

  @Override public int getNumberOfRecordsPerPage() {
   return delegate.getNumberOfRecordsPerPage();
  }

  // Same for other method.
}

If all else fails, pass in the class fields as separate parameters:

private HashMap<String, Integer> getPagination(int numberOfRecordsPerPage, int startAtRecordNumber) {

using some "special" value to indicate null, e.g. 0, since the conditional is a no-op if both parameters are zero.

1 Comment

offtopic: is there a specific reason why you deleted your answer on stackoverflow.com/questions/43141138/disable-thread-sleep/… ? I was about to upvote it, when I saw that you already deleted it yourself ?!
0

Both PagingRequestType could implement one common interface or extend one common class and you could take this "common part" as argument to your function. Although I don't know if you can modify your auto generated code in that way.

2 Comments

that's the problem on my side @LLL i can't modify WSDL file.
Then you could use reflection API but it could be overkill
0

If the option to make those two PagingRequestTypes to implement a common interface is impossiable, you could do it the reverse way, define a proxy type to wrap those two types:

public final class PagingRequestTypeProxy {
    private a.b.c.PagingRequestType abcPagingRequestType;

    private a.b.d.PagingRequestType abdPagingRequestType;

    public PagingRequestTypeProxy(PagingRequestType abcPagingRequestType) {
        this.abcPagingRequestType = abcPagingRequestType;
    }

    public PagingRequestTypeProxy(a.b.d.PagingRequestType abdPagingRequestType) {
        this.abdPagingRequestType = abdPagingRequestType;
    }

    public int getNumberOfRecordsPerPage() {
        return abcPagingRequestType != null ? abcPagingRequestType.getNumberOfRecordsPerPage() : abdPagingRequestType.getNumberOfRecordsPerPage();
    }

    public int getStartAtRecordNumber() {
        return abcPagingRequestType != null ? abcPagingRequestType.getStartAtRecordNumber() : abdPagingRequestType.getStartAtRecordNumber();
    }
}

And change the parameter type of getPagination to:

private HashMap<String, Integer> getPagination(PagingRequestTypeProxy pagingRequestType) {...}

Then you could use it like:

getPagination(new PagingRequestTypeProxy(abcPagingReqType));
getPagination(new PagingRequestTypeProxy(abdPagingReqType));

The cons is that there're extra codes to define PagingRequestTypeProxy, the pros is that logic of PagingRequestTypeProxy is very simple and easy to maintain, and you can put your biz code getPagination in one place.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.