Rejedai
Rejedai

Reputation: 53

How i can create Generic Method in Java?

I have two a conditions in the method:

if(urlSendModel.isHasPhoto()) {
    ArrayList<InputMediaPhoto> inputMediaPhotos = new ArrayList<>();

    for(String photoUrl : urlSendModel.getPhotos()){
        inputMediaPhotos.add(new InputMediaPhoto(photoUrl));
    }

    SendMediaGroup sendMediaGroup = new SendMediaGroup(message.chat().id(),
            inputMediaPhotos.toArray(new InputMediaPhoto[0]));

    bot.execute(sendMediaGroup);
}
if(urlSendModel.isHasVideo()){
    ArrayList<InputMediaVideo> inputMediaVideos = new ArrayList<>();

    for(String videoUrl : urlSendModel.getVideos()){
        inputMediaVideos.add(new InputMediaVideo(videoUrl));
    }

    SendMediaGroup sendMediaGroup = new SendMediaGroup(message.chat().id(),
            inputMediaVideos.toArray(new InputMediaVideo[0]));

    bot.execute(sendMediaGroup);
}

How can I create something like this or solve the problem in another way.

private <T extends InputMedia<T>> void sendMedia(Message message, ArrayList<String> urls) {
    ArrayList<T> inputMedia = new ArrayList<>();

    for(String url : urls){
        inputMedia.add(new T(url));
    }

    SendMediaGroup sendMediaGroup = new SendMediaGroup(message.chat().id(),
        inputMedia.toArray(new T[0]));

    bot.execute(sendMediaGroup);
}

I will be glad to any proposed solution.

Upvotes: 0

Views: 110

Answers (2)

BeUndead
BeUndead

Reputation: 3618

Both of the requirements here can be gotten around by passing the class into the method. I'll skip the additional details of what your method does, but, for instance:

<T> void doSomething(final Class<T> klass, final int length) {
    // Replace 'new T[10]'
    final T[] array = Array.newInstance(klass, length);

    final Constructor<T> constructor = klass.getConstructor();
    for (int i = 0; i < length; i++) {
        // Replace 'new T()'
        array[i] = constructor.newInstance();
    }
}

  • Note 1: The replacement for new T() was provided by @Vince Emigh.
  • Note 2: Exception handling is not considered here, so this will not compile as-is.
  • Note 3: If you need to use a different constructor to the one with no arguments, then it may well be simpler to accept a Function<Object[], T> which will convert the arguments you provide to an instance of the type.

Upvotes: 1

Karam Mohamed
Karam Mohamed

Reputation: 881

You can do something like creating the generic class first :

public class ClassList<T extends Object>{ private ArrayList<T> list; .... }
// or
public class ClassList<T extends InputMedia>{ private ArrayList<T> list; .... }

and then you cas use constructor or setter to affect a value to your attribute list of T

Upvotes: 0

Related Questions