I have such code in Java. How to make similar solution in C#.
Especially i'm interested how to implement the first and the last rows?
This code goes through specified package (that contains forms for Android and iOS) and returns Android form instance or iOS depending on getTargetPlatform()
public static <T extends Helpers> T getPage(Class pageInterface) throws Exception {
Set<Class<?>> allClasses = new Reflections("forms", new SubTypesScanner(false)).getSubTypesOf(Object.class);
for (Class pageClass : allClasses) {
if (pageInterface.isAssignableFrom(pageClass) && pageClass.getName().contains(String.format(".%1$s.", getTargetPlatform()))) {
return (T) pageClass.newInstance();
}
}
return (T) pageInterface.newInstance();}
Depending on your usecase, you could scan assemblies for your type. Example:
public static T GetPage<T>(Type pageInterface) where T : Helpers
{
// maybe you need to scan different assemblies, depending on your usecase
var allTypes = Assembly.GetExecutingAssembly().GetTypes();
foreach (var pageType in allTypes)
{
if (pageInterface.IsAssignableFrom(pageType) && pageType.Name.Contains(String.Format(".%1$s.", GetTargetPlatform())))
{
return (T)Activator.CreateInstance(pageType);
}
}
return (T)Activator.CreateInstance(pageInterface);
}
first row is like below:
public static T getPage<T>(Class pageInterface) where T: Helpers
and last row is just the same:
return (T) pageInterface.newInstance();
Related
I have an external model with about 20 classes, which I have to use, but cannot modify (in code below examples: FirstExtClass, SecondExtClass). In each of these classes there is an enum, which is its internal class (code below: TheSameEnum). I am translating it into my model, in which I want each of these enums to be just one type: EnumFromMyModel. I have solved it using many functions, that convert external enums to my enums (2 examples are below, but to completely convert them I need 20 copy-paste methods like below.
I am trying to create one generic function, that would take a class with TheSameEnum as parameter and return EnumFromMyModel (something like third function in code below).
private static EnumFromMyModel
convertFirstEnumFromExternalModelToEnumFromMyModel(FirstExtClass.TheSameEnum input) {
return input.equals(FirstExtClass.TheSameEnum.FIRST_VALUE) ?
EnumFromMyModel.FIRST_VALUE :
EnumFromMyModel.SECOND_VALUE;
}
private static EnumFromMyModel convertSecondEnumFromExternalModelToEnumFromMyModel(SecondExtClass.TheSameEnum input) {
return input.equals(SecondExtClass.TheSameEnum.FIRST_VALUE) ?
EnumFromMyModel.FIRST_VALUE :
EnumFromMyModel.SECOND_VALUE;
}
private static <T> EnumFromMyModel genericConvert(T input) {
return input.equals(/*????*/) ?
ScopeUsageLimit.FIRST_VALUE :
ScopeUsageLimit.SECOND_VALUE;
}
My question is: is this possible to create such generic function?
Here is some sample code for you. You need toGeneric method.
public class App {
public static void main(String[] args) {
Generic genericFirst1 = toGeneric(Specialized1.FIRST);
Generic genericFirst2 = toGeneric(Specialized2.FIRST);
assert genericFirst1 == genericFirst2;
Specialized1 specialized1 = toSpecialized(Generic.SECOND, Specialized1.class);
assert specialized1 == Specialized1.SECOND;
}
private static <T extends Enum<?>>T toSpecialized(Generic v, Class<T> specialized1Class) {
try {
return (T) specialized1Class.getField(v.name()).get(null);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
return null;
}
private static Generic toGeneric(Enum<?> e) {
return Generic.valueOf(e.name());
}
enum Generic {
FIRST, SECOND
}
enum Specialized1 {
FIRST, SECOND
}
enum Specialized2 {
FIRST, SECOND
}
}
If I understand the question correctly, you are looking for something like the below code:
private static <T extends Enum> EnumFromMyModel genericConvert(T input) {
return input.name().equals(/*????*/) ?
EnumFromMyModel.FIRST_VALUE :
EnumFromMyModel.SECOND_VALUE;
}
You can do this if the enum value names of your and the external model are equal by converting the external enum to String and then converting this string back to your enum like this:
private static EnumFromMyModel genericConvert(Enum<?> input) {
return EnumFromMyModel.valueOf(input.name());
}
This will throw an IllegalArgumentException in case no matching enum from EnumFromMyModel can be found.
Still, i'd rather not use this and create all those 20 methods you mentioned and explicitly convert each enum value in a compile time safe manner. IMHO, the benefit of having compile time safety outweighs the benefit of having to write a little less code.
I have two ArrayLists - ArrayList1 and ArrayList2. Each of them is filled with objects - Object1 and Object2, respectively.
Both of these objects have method 'getText'.
Object1:
public String getText() { return "1";}
Object2:
public String getText() { return "2";}
At certain point I would like to loop through each of these lists using the same method (just with different parameter).
loopThroughList(1)
loopThroughList(2)
What is the syntax if I want to call a method, but I don't know which object it is going to be? This is the code I have so far:
for (Object o : lists.getList(listNumber)) {
System.out.println(o.getText());
}
It says Cannot resolve method getText. I googled around and found another solution:
for (Object o : lists.getList(listNumber)) {
System.out.println(o.getClass().getMethod("getText"));
}
But this gives me NoSuchMethodException error. Even though the 'getText' method is public.
EDIT: To get the correct list, I am calling the method 'getList' of a different object (lists) that returns either ArrayList1 or ArrayList2 (depending on the provided parameter).
class Lists
public getList(list) {
if (list == 1) {
return ArrayList1;
}
else if (list == 2) {
return ArrayList2;
}
}
Define an interface for the getText method
public interface YourInterface {
String getText();
}
Implement the interface on the respective classes
public class Object1 implements YourInterface {
#Override
public String getText() {
return "1";
}
}
public class Object2 implements YourInterface {
#Override
public String getText() {
return "2";
}
}
Modify your getList method to return List<YourInterface>
public static List<YourInterface> getList(int list){
List<YourInterface> result = new ArrayList<>();
if(list == 1){
// your initial type
List<Object1> firstList = new ArrayList<>();
result.addAll(firstList);
} else {
// your initial type
List<Object2> secondList = new ArrayList<>();
result.addAll(secondList);
}
return result;
}
Declaration for loopThroughList
public static void loopThroughList(List<YourInterface> list){
list.forEach(yourInterface -> System.out.println(yourInterface.getText()));
}
Sample usage.
public static void main(String[] args) {
loopThroughList(getList(1));
loopThroughList(getList(2));
}
Interfaces work great here, but there a couple of other options if you're dealing with legacy code and cannot use interfaces.
First would be to cast the list items into their respective types:
for (Object o : lists.getList(listNumber)) {
if(o instanceof Object1) {
Object1 o1 = (Object1)o;
System.out.println(o1.getText());
}
else if(o instanceof Object2) {
Object1 o2 = (Object2)o;
System.out.println(o2.getText());
}
else {
System.out.println("Unknown class");
}
}
You can also use reflection to see if the object has a getText method and then invoke it:
for (Object o : lists.getList(listNumber)) {
try {
System.out.println(o.getClass().getDeclaredMethod("getName").invoke(o));
}
catch(Exception e) {
System.out.println("Object doesn't have getText method");
}
}
This is awful. Can you elaborate on what specifically you are trying to do? Java is strong typed by design, and you are trying to get around it. Why? Instead of Object, use the specific class, or interface as previously suggested. If that's not possible, and you must use lists of Objects, use instanceof and casting eg:
for (Object o : lists.getList(listNumber)) {
if (o instanceof Object1) {
Object1 o1 = (Object1) o;
System.out.println(o1.getText());
} else if (o instanceof Object2) {
Object2 o2 = (Object2) o;
System.out.println(o2.getText());
}
}
This is where interfaces come in.
interface HasText {
public String getText();
}
class Object1 implements HasText {
#Override
public String getText() {
return "1";
}
}
class Object2 implements HasText {
#Override
public String getText() {
return "2";
}
}
private void test() {
List<HasText> list = Arrays.asList(new Object1(), new Object2());
for (HasText ht : list) {
System.out.println(ht);
}
}
If one of your objects is not in your control you can use a Wrapper class.
class Object3DoesNotImplementHasText {
public String getText() {
return "3";
}
}
class Object3Wrapper implements HasText{
final Object3DoesNotImplementHasText it;
public Object3Wrapper(Object3DoesNotImplementHasText it) {
this.it = it;
}
#Override
public String getText() {
return it.getText();
}
}
private void test() {
List<HasText> list = Arrays.asList(new Object1(), new Object2(), new Object3Wrapper(new Object3DoesNotImplementHasText()));
for (HasText ht : list) {
System.out.println(ht);
}
}
Just to add more to this answer and give you some more to think on this (Will try to do it in a simple, non-formal way). Using interfaces is the proper way of doing such operation. However, I want to stand on the "bad idea":
for (Object o : lists.getList(listNumber)) {
System.out.println(o.getClass().getMethod("getText"));
}
What you are doing here, is using a mechanism called Reflection:
Reflection is a feature in the Java programming language. It allows an
executing Java program to examine or "introspect" upon itself, and
manipulate internal properties of the program. For example, it's
possible for a Java class to obtain the names of all its members and
display them.
What you actually attempted, is using that mechanism, to retrieve the method through a Class reflection object instance of your Class (sounds weird, isn't it?).
From that perspective, you need to think that, if you want to invoke your method, you now have, in a sense, a meta-Class instance to manipulate your objects. Think of it like an Object that is one step above your Objects (Similarly to a dream inside a dream, in Inception). In that sense, you need to retrieve the method, and then invoke it in a different (meta-like) way:
java.lang.reflect.Method m = o.getClass().getMethod("getText");
m.invoke(o);
Using that logic, you could possibly iterate through the object list, check if method exists, then invoke your method.
This is though a bad, BAD idea.
Why? Well, the answer relies on reflection itself: reflection is directly associated with runtime - i.e. when the program executes, practically doing all things at runtime, bypassing the compilation world.
In other words, by doing this, you are bypassing the compilation error mechanism of Java, allowing such errors happen in runtime. This can lead to unstable behavior of the program while executing - apart from the performance overhead using Reflection, which will not analyze here.
Side note: While using reflection will require the usage of Checked Exception handling, it still is not a good idea of doing this - as you practically try to duck tape a bad solution.
On the other hand, you can follow the Inheritance mechanism of Java through Classes and Interfaces - define an interface with your method (let's call it Textable), make sure that your classes implement it, and then use it as your base object in your list declaration (#alexrolea has implemented this in his answer, as also #OldCurmudgeon has).
This way, your program will still make the method call decision making at Runtime (via a mechanism called late binding), but you will not bypass the compilation error mechanism of Java. Think about it: what would happen if you define a Textable implementation without providing the class - a compile error! And what if you set a non-Textable object into the list of Textables? Guess what! A compile error again. And the list goes on....
In general, avoid using Reflection when you are able to do so. Reflection is useful in some cases that you need to handle your program in such a meta-way and there is no other way of making such things. This is not the case though.
UPDATE: As suggested by some answers, you can use instanceof to check if you have a specific Class object instance that contains your method, then invoke respectively. While this seems a simple solution, it is bad in terms of scaling: what if you have 1000 different classes that implement the same method you want to call?
your objects have to implement a common interface.
interface GetTextable {
String getText();
}
class One implements GetTextable {
private final String text;
public One(final String text) {
this.text = text;
}
public String getText() {
return this.text;
}
}
class Two implements GetTextable {
private final String text;
public Two(final String text) {
this.text = text;
}
public String getText() {
return this.text;
}
}
#Test
public void shouldIterate() throws Exception {
List<GetTextable> toIterate = Arrays.asList(new One("oneText"), new Two("twoText"));
for(GetTextable obj: toIterate) {
System.out.println(obj.getText());
}
}
Using GraphStage is recommended in Akka Streams, but I could not find any documentation on using the getStageActor() method in Java (all of the documentation that I have found used Scala).
How can I convert the following code to Java?
lazy val self: StageActor = getStageActor(onMessage)
and
private def onMessage(x: (ActorRef, Any)): Unit =
{
x match {
case (_, msg: String) =>
log.info("received msg, queueing: {} ", msg)
messages = messages.enqueue(msg)
pump()
}
}
According to the getStageActor method documentation, it accepts a value of type
scala.Function1<scala.Tuple2<ActorRef,java.lang.Object>, scala.runtime.BoxedUnit>
which in Scala looks like
((ActorRef, AnyRef)) => Unit
In Java this type would be semantically equivalent (using the Function interface) to
Function<Tuple<ActorRef, Object>, Void>
where Tuple<A, B> is a class which contains two values of types A and B.
Therefore, to call the getStageActor method, you need to create a value of the aforementioned type. You can do it directly by constructing an instance of a class extending AbstractFunction1:
import scala.Function1;
import scala.Tuple2;
import scala.runtime.AbstractFunction1;
import scala.runtime.BoxedUnit;
getStateActor(new AbstractFunction1<Tuple2<ActorRef, Object>, BoxedUnit>() {
#Override
public BoxedUnit apply(Tuple2<ActorRef, Object> args) {
return BoxedUnit.UNIT;
}
});
If you use Java 8, there are syntactically nicer ways to do it using lambda expressions.
If you use Scala 2.12+, then scala.Function1 is a functional interface, and you can use lambda expressions directly:
getStateActor((args: Tuple2<ActorRef, Object>) -> BoxedUnit.UNIT);
If you use an older version of Scala, then due to the way traits are compiled, Function1 is not a functional interface, and you will need to use the scala-java8-compat library. With it, the code looks like
import static scala.compat.java8.JFunction.*;
getStateActor(func((args: Tuple2<ActorRef, Object>) -> BoxedUnit.UNIT));
Then, to implement the logic of the function, you can access elements of the tuple using the _1() and _2() methods:
(args: Tuple2<ActorRef, Object>) -> {
Object msg = args._2();
if (msg instanceof String) {
log.info("received msg, queueing: {} ", msg);
messages = messages.enqueue((String) msg);
pump();
}
return BoxedUnit.UNIT;
}
This is a direct translation of the logic that you wanted to convert.
I have not used getStageActor() before, hence can't provide much help there. As to code conversion from Scala to Java in general, if requirement allows, package a jar containing the classes of the Scala version for the Java app to use; otherwise consider using a Java decompiler (e.g. cfr, procyon) to decompile your Scala-compiled classes and refine the decompiled Java code as needed.
For example decompiling the following dummy Scala code would help reveal a skeletal Java way of lazy val and pattern matching:
class Foo {
lazy val self = dummy(bar(_: String))
def dummy(u: Unit) = 1
private def bar(x: String): Unit = {
x match {
case "blah" => println(s"x = $x")
}
}
}
As shown in the decompiled code below, lazy val is done in Java with the value wrapped inside a synchronized block with a bitmap$0 boolean flag, and pattern matching gets converted to if and MatchError exception:
$ java -jar /path/to/decompiler/cfr_0_125.jar Foo.class
private int self;
private volatile boolean bitmap$0;
private int self$lzycompute() {
Foo foo = this;
synchronized (foo) {
if (!this.bitmap$0) {
new Serializable(this){
public static final long serialVersionUID = 0L;
private final /* synthetic */ Foo $outer;
public final void apply(String x$1) {
this.$outer.Foo$$bar(x$1);
}
{
if ($outer == null) {
throw null;
}
this.$outer = $outer;
}
};
this.self = this.dummy(BoxedUnit.UNIT);
this.bitmap$0 = true;
}
return this.self;
}
}
public int self() {
return this.bitmap$0 ? this.self : this.self$lzycompute();
}
public int dummy(BoxedUnit u) {
return 1;
}
public void Foo$$bar(String x) {
String string = x;
if ("blah".equals(string)) {
Predef$.MODULE$.println((Object)new StringContext(
(Seq)Predef$.MODULE$.wrapRefArray((Object[])new String[]{"x = ", ""})
).s((Seq)Predef$.MODULE$.genericWrapArray((Object)new Object[]{x}))
);
BoxedUnit boxedUnit = BoxedUnit.UNIT;
return;
}
throw new MatchError((Object)string);
}
I'm relatively new to Java and generics. I'm trying to understand if I'm doing something wrong or not in writing a generic method. I have the following code (greatly simplified):
public class ContentIniter {
public ContentType getContentType();
}
public interface Content {
}
public class Show implements Content {
}
public class Movie implements Content {
}
public enum ContentType {
Movie, Show
}
public class Channel {
public List<Show> getShows() {
return getContentByType(ContentType.Show)
}
public List<Movie> getMovies() {
return getContentByType(ContentType.Movie)
}
private <T> List<T> getContentByType(ContentType contentType) {
List<T> typeContents = Lists.newArrayList();
List<ContentIniter> allContentIniters = someMethod(); // Returns initers for both shows and movies
for (Content contentIniter : allContentIniters) {
if (contentIniter.getContentType().equals(contentType)) {
switch (contentType) {
case Movie:
typeContents.add((T) new Movie(contentIniter));
break;
case Show:
typeContents.add((T) new Show(contentIniter));
break;
}
}
}
return typeContents;
}
}
My question relates to the line:
typeContents.add((T) new Movie(contentIniter));
The only way I've been able to get the code to compile is if I cast the content object to T. But that seems yucky to me (and I don't understand why the compiler can't infer the type based on the calls). Moreover, even though the code works, IntelliJ complains of an unchecked cast.
Is there a better way to write the generic method?
UPDATE: Screwed up the code a bit when I tried to simplify it. Fixed the reference to typeContents. Also, I added a bit more complexity so that it better reflects the reality, in hopes of explaining why I wasn't simply checking for instanceof.
UPDATE 2: Realized there was yet another error...ContentIniter doesn't implement Content. It's also worth noting, ContentIniter is just a made up object. If it seems weird, think of it as an Event or other Strategy that Content objects use to delegate certain behaviors.
You're not using generics properly, you're mixing them with your enumeration when it's really not necessary. Ideally you would be calling getContentByType<Show>() and then determine the list of the correct type from allContents using reflection.
Try something more along the lines of like (untested):
private <T> List<T> getContents() {
List<T> typeContents = Lists.newArrayList();
List<Content> allContents = someMethod(); // Returns both shows and movies
for (Content content : allContents) {
if (content instanceof T) {
typeContents.add((T) content);
}
}
return typeContents;
}
And call:
List<Show> shows = getContents<Show>();
You can then restrict the types that are called on it to only those that extend Content.
private <T extends Content> List<T> getContents() {
...
}
Actually the answer is simpler than you think : you just have to check whether your instance of Content is a Show or a Movie to make your compiler happy :
if (content instanceof Movie)
contents.add((Movie) content);
if (content instanceof Show)
contents.add((Show) content);
Anyway, I would say that the way that you wrote your generic method is correct. But since there is a native way to check for the type of an instance (instanceof), you should use it :)
EDIT : I still think you should use instanceof.
Plus, you should use a List<Content> instead of a List<ContentIniter>, because Content is a more global type : if someone comes up with another implementation of Content, he won't have to change your code. Actually, you're doing the same thing when you use the Interface List instead of an ArrayList for example, because List is less specific than ArrayList.
Also, using an enum is not a mistake : if you want to use one, you can. But it shouldn't be used to determine the type of an instance. The type of an instance is contained in the instance itself, period. Still, I'll say that Daniel Imms' solution is more elegant than mine, and takes better advantage of Java type features.
public interface Content {
public STContentType getContentType();
}
public class ContentIniter implements Content {
}
// You can keep the enum, as long as it's not used
// to check for the type of an instance of ContentIniter
public enum ContentType {
Movie, Show
}
public class Show implements Content {
}
public class Movie implements Content {
}
public class Channel {
public List<Show> getShows() {
return getContentByType(ContentType.Show)
}
public List<Movie> getMovies() {
return getContentByType(ContentType.Movie)
}
private <T> List<T> getContentByType(ContentType contentType) {
List<T> typeContents = Lists.newArrayList();
// Using more generic type Content
List<Content> allContentIniters = someMethod(); // Returns initers for both shows and movies
for (Content contentIniter : allContentIniters) {
// If it's a Show and I asked for Shows
if (contentIniter instanceof Show && contentType == ContentType.Show)) {
typeContents.add(contentIniter);
}
// If it's a Movie and I asked for Movies
if (contentIniter instanceof Movie && contentType == ContentType.Movie){
typeContents.add(contentIniter);
}
}
return typeContents;
}
}
Use of enum seems strange here and the way you do lost advantage of using generics.
The initer things is making things even more strange and messy.
It may looks more natural with something like this:
public interface Content {
}
public class Show implements Content {
}
public class Movie implements Content {
}
//......
private <T extends Content> List<T> getContentByType(Class<T> contentType) {
List<T> result = Lists.newArrayList();
List<Content> allContents = someMethod(); // ContentIniter is just a mess
// Get all content you have!
for (Content content: contents) {
if (contentType.isAssignableFrom(content.getClass())) {
result.add(content);
}
}
return result;
}
The way to use is
List<Show> result = channel.getContent(Show.class);
I removed my original answer after the code example change.
I really don't think you can avoid the cast and the #SuppressWarnings("unchecked").
As long as you know what you're doing, this is probably the best solution.
The alternative is to do without the getByContentType method and just have a bit of duplicated logic on the getShows() and getMovies() methods.
For example:
public List<Show> getShows() {
List<Show> shows = new ArrayList<Show>();
List<ContentIniter> allContentIniters = someMethod();
for(ContentIniter initer: allContentIniters) {
if(initer.getContentType().equals(ContentType.Show)) {
shows.add(new Show(initer));
}
}
return shows;
}
public List<Movie> getMovies() {
List<Movie> movies = new ArrayList<Movie>();
List<ContentIniter> allContentIniters = someMethod();
for(ContentIniter initer: allContentIniters) {
if(initer.getContentType().equals(ContentType.Movie)) {
movies.add(new Movie(initer));
}
}
return movies;
}
I am using Flex 3 and make a call through a RemoteObject to a Java 1.6 method and exposed with BlazeDS and Spring 2.5.5 Integration over a SecureAMFChannel. The ActionScript is as follows (this code is an example of the real thing which is on a separate dev network);
import com.adobe.cairngorm.business.ServiceLocator;
import mx.collections.ArrayCollection;
import mx.rpc.remoting.RemoteObject;
import mx.rpc.IResponder;
public class MyClass implements IResponder
{
private var service:RemoteObject = ServiceLocator.getInstance().getRemoteOjbect("mySerivce");
public MyClass()
{
[ArrayElementType("Number")]
private var myArray:ArrayCollection;
var id1:Number = 1;
var id2:Number = 2;
var id3:Number = 3;
myArray = new ArrayCollection([id1, id2, id3]);
getData(myArray);
}
public function getData(myArrayParam:ArrayCollection):void
{
var token:AsyncToken = service.getData(myArrayParam);
token.addResponder(this.responder); //Assume responder implementation method exists and works
}
}
This will make a call, once created to the service Java class which is exposed through BlazeDS (assume the mechanics work because they do for all other calls not involving Collection parameters). My Java service class looks like this;
public class MySerivce {
public Collection<DataObjectPOJO> getData(Collection<Long> myArrayParam) {
//The following line is never executed and throws an exception
for (Long l : myArrayParam) {
System.out.println(l);
}
}
}
The exception that is thrown is a ClassCastException saying that a java.lang.Integer cannot be cast to a java.lang.Long. I worked around this issue by looping through the collection using Object instead, checking to see if it is an Integer, cast it to one, then do a .longValue() on it then add it to a temp ArraList. Yuk.
The big problem is my application is supposed to handle records in the billions from a DB and the id will overflow the 2.147 billion limit of an integer. I would love to have BlazeDS or the JavaAdapter in it, translate the ActionScript Number to a Long as specified in the method. I hate that even though I use the generic the underlying element type of the collection is an Integer. If this was straight Java, it wouldn't compile.
Any ideas are appreciated. Solutions are even better! :)
Please read the following threads related to your issue. You can find there some workarounds.
https://bugs.adobe.com/jira/browse/BLZ-115
https://bugs.adobe.com/jira/browse/BLZ-305
You can also change the argument on the Java side to expect a Long[] rather than a Collection<Long>. Because the native Java array is strongly typed, it deserializes correctly.
Flex serializes an ArrayCollection of Numbers to an ArrayCollection<Integer> in Java.
Since Adobe's ArrayCollection extends ArrayList, you can run the Collection through the following function. This should produce a List of Long values.
public class TransformUtils {
public static final <T extends Number> List<Long> toLongList(Collection<T> values) {
List<Long> list = new ArrayList();
for (T value : values) {
list.add(value.longValue());
}
return list;
}
}
public class MySerivce {
public Collection<DataObjectPOJO> getData(Collection<Long> myArrayParam) {
myArrayParam = TransformUtils.toLongList(myArrayParam);
for (Long l : myArrayParam) {
System.out.println(l);
}
}
}
Guava :)
public static final <T extends Number> List<Long> toLongList(Collection<T> values) {
return Lists.newArrayList(new Function<T, Long>() {
#Override public Long apply(T value) {
return value.longValue(); }));}