generics

Generics and ReadObject

前提是你 提交于 2020-01-14 01:36:15
问题 I have a simple server that uses generics and object serialization. (T is the input format, U is the output format). A simplified version that only deals with input is shown below: public class Server <T, U> implements Runnable { @override public void run () { try (ObjectInputStream inReader = new ObjectInputStream (this.connection.getInputStream ())) { T lastObj; while (true) { lastObj = (T) inReader.readObject (); System.out.println (lastObj.getClass ().getName ()); if (null != lastObj) {

Generics and ReadObject

◇◆丶佛笑我妖孽 提交于 2020-01-14 01:36:11
问题 I have a simple server that uses generics and object serialization. (T is the input format, U is the output format). A simplified version that only deals with input is shown below: public class Server <T, U> implements Runnable { @override public void run () { try (ObjectInputStream inReader = new ObjectInputStream (this.connection.getInputStream ())) { T lastObj; while (true) { lastObj = (T) inReader.readObject (); System.out.println (lastObj.getClass ().getName ()); if (null != lastObj) {

Restrictions around Protocols and Generics in Swift

主宰稳场 提交于 2020-01-13 19:31:12
问题 There are scenarios when two interacting sets of generic classes is a good design pattern. A simple example is the Observable-Observer pattern. The observable posts events to the observer but the pattern is the same no matter what type of event that is being observed. My first thought was that the preferred way would be to define two generic protocols. This should offer the smallest coupling, which tends to be good as the code base grows. protocol ProtocolObserver { typealias EventType func

Compiler fails converting a constrained generic type

对着背影说爱祢 提交于 2020-01-13 19:24:08
问题 I have a class that has a Generic type "G" In my class model i have public class DetailElement : ElementDefinition Let's say i have a method like this public void DoSomething<G>(G generic) where G : ElementDefinition { if (generic is DetailElement) { ((DetailElement)generic).DescEN = "Hello people"; //line 1 ////// ElementDefinition element = generic; ((DetailElement)element).DescEN = "Hello again"; //line 3 ////// (generic as DetailElement).DescEN = "Howdy"; //line 5 } else { //do other

Java generic enum functionality using java 8 default methods and utility class

前提是你 提交于 2020-01-13 19:14:07
问题 Below is the failed attempt which I came up with, referring to Java Generic Enum class using Reflection. Wanted to find a better way to do this. Couple of issues I find with this approach: Each and every time I need to pass the class type. Example - EnumUtility.fromKey(Country.class, 1) fromSet is duplicated in both City and Country ‌ public enum City implements BaseEnumInterface { TOKYO(0), NEWYORK(1); private final int key; public static Set<Integer> fromValue(Set<City> enums) { return

Cannot convert functional interface with generic method into lambda expression

半腔热情 提交于 2020-01-13 18:11:33
问题 Cannot convert functional interface with generic method into lambda expression. Following code is working. It is without lambda expression i.e. using anonymous class. public interface Pro{ public <T> void callee(T t); } public void fun(Pro obj){} public void implement() { fun(new Pro() { @Override public <Integer> void callee(Integer t){} }); } I cannot understand how to use lambda expression instead of anonymous class. After trying it myself I used the hint shown in netbeans. I used the

Cannot convert functional interface with generic method into lambda expression

ⅰ亾dé卋堺 提交于 2020-01-13 18:11:10
问题 Cannot convert functional interface with generic method into lambda expression. Following code is working. It is without lambda expression i.e. using anonymous class. public interface Pro{ public <T> void callee(T t); } public void fun(Pro obj){} public void implement() { fun(new Pro() { @Override public <Integer> void callee(Integer t){} }); } I cannot understand how to use lambda expression instead of anonymous class. After trying it myself I used the hint shown in netbeans. I used the

Casting generic parameter to and from integer

社会主义新天地 提交于 2020-01-13 16:20:08
问题 I want to write generic class which is intended to work with built-in types like byte and ushort . In internal computations I need to cast generic type to integer and back to generic type. I found the way to compile such code, for example: class Test<T> where T : struct, IConvertible { public static T TestFunction(T x) { int n = Convert.ToInt32(x); T result = (T)Convert.ChangeType(n, typeof(T)); return result; } } I think that using such conversions may significantly reduce performance, if

How do I pass function delegates to be called with LINQ? [closed]

北城以北 提交于 2020-01-13 14:01:10
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 8 years ago . I think I'm asking the right question here... I have 4 stored procedures that return different subsets of the same data. I map this data to the same object in my server application. I've set my code up as follows

Generics and wildcards with collections in Java

故事扮演 提交于 2020-01-13 12:17:38
问题 In a test class using AssertJ, I have code similar to the following: public void someTest() { assertThat(getNames()).has(sameNamesAs(getExpectedNames())); assertThat(getNames()).doesNotHave(sameNamesAs(getOtherNames())); } private List<String> getNames() { return null; } private List<String> getExpectedNames() { return null; } private List<String> getOtherNames() { return null; } private Condition<List<String>> sameNamesAs(List<String> rhs) { return new Condition<List<String>>("same names as