I'm looking for a way to convert an array of one generic type to another. Here's my use case:
I have a base library with the following Composite:
// leaf
public interface Job<T extends JobResult> {
public T doJob();
}
public class CompositeJob<T extends JobResult> implements Job<T>{
private Job<T>[] jobs;
// ...
}
// component
public interface Worker<T extends JobResult> {
public T doWork();
public Job<T>[] getJobs();
public void addJob(Job<T> job);
}
public class HardWorker<T extends JobResult> implements Worker<T> {
public T doWork() {
// ...
}
}
I also have a custom library that imports the base library, which uses a more domain-specific name for a Job, a "Customer". I'd like users of my custom library to only interface through my Customer types, so I can change the implementation of the base library without disturbing my public API.
I'm hindered because Worker.getJobs() returns an array of Job[]. I'd like to write a method that can convert Job[] to WorkItem[] to prevent exposure of the base library's types, but I'm not sure how to write that conversation method in Java.
Here's the signature of the method I want:
public WorkItem<T1>[] convert(Job<T>[] jobs) {
for (Job<T> job : jobs) {
// convert a T to a T1
// create a new WorkItem using the Job as input
}
}

WorkItem.WorkItemfrom the properties of aJob.