How to valid the type of generic parameter in Java (with out using reflection if possible) ?
The target is something like C# allow to do:
public <T> void doStaff() {
if(T is Type1) {
}
if(T is Type2) {
}
}
How to write method like that in Java ?
...
I am building a entity Repository and I have an interface, IIdentifiable<T>. So entities which are identified by Guids, for example, implement IIdentifiable<Guid> with public Guid Id { get; }. So far, so good.
I have an interface of IRepository<T, TIdentifier> where T : IIdentifiable<TIdentifier>.
It seems to me that the TIdentifier ...
I was looking for ways to do lazy initialization and found Lazy<T> which is included in .NET 4.
I was thinking of rolling my own implementation of Lazy<T> for .NET 3.5 (with a simpler multi-thread policy), and I bumped into the following problem:
Lazy has basically two types of constructors:
class Lazy<T> {
public Lazy(){...} // ...
I am writing some simple Vector and Matrix classes. They look like this:
// Vector with Floats
case class Vector3f(x: Float, y: Float, z: Float) {
def +(v: Vector3f) = Vector3f(x + v.x, y + v.y, z + v.z)
}
// Vector with Doubles
case class Vector3d(x: Double, y: Double, z: Double) {
def +(v: Vector3d) = Vector3d(x + v.x, y + v.y, ...
Hi,
Here is my simple code:
T a;
T b;
if (a == b)
// sth.
else
// sth. else
When I try to compile, I get an error saying the == operator is invalid for generic types. So I have to use the object.Equals() method.
Doesn't the == operator actually call the Equals method of object? Why can I use the Equals method of two generic types b...
I am trying to create a generic simulation runner. Each simulation implements a variety of interfaces. Eventually, it will pick up simulation types via DLLs at run time, so I will have no way of knowing the types beforehand.
My Currrent Code:
public class SimulationRunner<TSpace, TCell>
where TSpace : I2DState<TCell>
where TCe...
I would like to write a cast method in Eiffel which takes 'the type to cast to' as a type parameter. Is there a way to pass a type into a method in Eiffel.
The only alternative I can think of is to create a new class for the conversion. Something like:
class
CAST [G, H]
feature
cast (in: LIST [G]): LIST [H]
do
...
I have an old untyped class pre Java1.5 and need to refactor it using Java Generics to get more type security in the code.
Old Code looks like this:
class Foo {
void setInput(Object input) {...}
}
Now this input could be some object but it may also be an array, nasty stuff. Making a generic version of it seems not to be trivial, I...
Generics in Java is noisy sometimes. The parameterized types are thrown out by the compiler anyway after compile, so my question is, is there really any drawbacks in ignoring them, as long as I declare the type of the reference to include parameterized types? e.g.,
Map<String, Map<Integer, String>> myMap = new HashMap<String, Map<Intege...
Aside from the added complexity, do you think that the inclusion of the Generics Mechanism in Java 5.0 was worthwhile?
...
I wish to construct a list of anonymous types constructed by iterating through two other lists in a nested loop.
var myList = new List<???>();
foreach (object x in GetAllX())
{
if (Process(x))
{
foreach (object y in GetAllY(x))
{
myList.Add(new {
X = x,
Y = y
...
For a function as below:
def reverse[T](a: Array[T]): Array[T] = {
val b = new Array[T](a.length)
for (i <- 0 until a.length)
b(i) = a(a.length -i - 1)
b
}
I am getting "error: cannot find class manifest for element type T" from line 2.
Is there anyway to solve this?
...
Here's my test code:
var container = MockRepository.GenerateMock<UnityContainer>();
container.Expect(e => e.RegisterType<IEventAggregator, EventAggregator>(
Arg<ContainerControlledLifetimeManager>.Is.Anything));
Basically I'm going to assert that a class that takes a IUnityContainer interface sets up the container in an expected wa...
I currently have a mapping setup to convert from one base class to another base class. I am mapping a customer class to a thrid party control. The values are similiar but there is enough differences that I can't reuse the thrid party control.
_converters = new Dictionary<Type, Func<AnnotationBase, AnnotationMark>>();
_converters.Add( ty...
I'd like to be able to create a static generic type with a base type constraint like
public static class Manager<T> where T : HasId
{
public static T GetSingleById(ref List<T> items, Guid id)
{
// the Id is a property provided by HasId
return (from i in items where i.Id == id select i).SingleOrDefault();
}
}
...
I want to convert from
public class Party
{
public string Type { get; set; }
public string Name { get; set; }
public string Status { get; set;}
}
and convert to
public class Contact
{
public string Type { get; set; }
public string Name { get; set; }
public string Status { get; set;...
I admit that I don't have a lot of experience with using Java Generics.
Right now, I'm retrofitting some old code with Generics to reduce/simplify existing code written by a colleague who has since left the company.
Basically, the system I'm working on has 6 request types: 1 General, and 5 specific that inherit from General. Each requ...
I've added some of the new Generics into my Delphi 2009 program.
In the Structure window of the Delphi IDE, I'm getting a bunch of errors of the form:
'TList` 1' does not contain a member named 'JumpID' at line 1031 (1031:57)
My declarations and lines seem fine to me. And my program Builds without any errors and runs without problem...
I've gotten myself in a pickle, and could use the help of a guru...
I have a Journal that records entries for different types:
Journal(Of ParentT)
- Parent could be Customer, Address, other classes
The constructor of the Journal requires knowledge of the Type parameter:
Public Sub New(Parent as ParentT)
In my consuming form, I take ...
I am converting my datatable to c# generic list.
DataTable dt = mydata();
List<DataRow> list = dt.AsEnumerable().ToList();
Now how can i convert this list to json using json.net? Any suggestion.
Sample of json format should be like this,
{"Table" : [{"userid" : "1","name" : "xavyTechnologies","designation" : "",
"phone" : "9999999...