Java N 元组实现
我刚刚制作了一个类型安全的 Java n 元组。
我正在使用一些非常规方法来实现类型安全(我只是为了好玩而这样做)。
有人可以提供一些改进建议或一些可能的缺陷吗?
public class Tuple {
private Object[] arr;
private int size;
private static boolean TypeLock = false;
private static Object[] lastTuple = {1,1,1}; //default tuple type
private Tuple(Object ... c) {
// TODO Auto-generated constructor stub
size=c.length;
arr=c;
if(TypeLock)
{
if(c.length == lastTuple.length)
for(int i = 0; i<c.length; i++)
{
if(c[i].getClass() == lastTuple[i].getClass())
continue;
else
throw new RuntimeException("Type Locked");
}
else
throw new RuntimeException("Type Locked");
}
lastTuple = this.arr;
}
public static void setTypeLock(boolean typeLock) {
TypeLock = typeLock;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (this == obj)
return true;
Tuple p = (Tuple)obj;
for (int i = 0; i < size; i++)
{
if (p.arr[i].getClass() == this.arr[i].getClass())
{
if (!this.arr[i].equals(p.arr[i]))
return false;
}
else
return false;
}
return true;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
int res = 17;
for(int i = 0; i < size; i++)
res = res*37+arr[i].hashCode();
return res;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return Arrays.toString(arr);
}
public static void main(String[] args) {
HashMap<Tuple,String> birthDay = new HashMap<Tuple,String>();
Tuple p = new Tuple(1,2,1986);
Tuple.setTypeLock(true);
Tuple p2 = new Tuple(2,10,2009);
Tuple p3 = new Tuple(1,2,2010);
Tuple p4 = new Tuple(1,2,2010);
birthDay.put(p,"Kevin");
birthDay.put(p2,"Smith");
birthDay.put(p3,"Sam");
birthDay.put(p4, "Jack");
System.out.println(birthDay);
System.out.println(birthDay.get(new Tuple(1,2,1986)));
birthDay.put(new Tuple(1,2,""),"");
}
}
I just made a Java n-tuple which is type-safe.
I'm using some unconventional methods to achieve type-safety (I just made it for fun).
Can someone can give some input on improving it or some possible flaws.
public class Tuple {
private Object[] arr;
private int size;
private static boolean TypeLock = false;
private static Object[] lastTuple = {1,1,1}; //default tuple type
private Tuple(Object ... c) {
// TODO Auto-generated constructor stub
size=c.length;
arr=c;
if(TypeLock)
{
if(c.length == lastTuple.length)
for(int i = 0; i<c.length; i++)
{
if(c[i].getClass() == lastTuple[i].getClass())
continue;
else
throw new RuntimeException("Type Locked");
}
else
throw new RuntimeException("Type Locked");
}
lastTuple = this.arr;
}
public static void setTypeLock(boolean typeLock) {
TypeLock = typeLock;
}
@Override
public boolean equals(Object obj) {
// TODO Auto-generated method stub
if (this == obj)
return true;
Tuple p = (Tuple)obj;
for (int i = 0; i < size; i++)
{
if (p.arr[i].getClass() == this.arr[i].getClass())
{
if (!this.arr[i].equals(p.arr[i]))
return false;
}
else
return false;
}
return true;
}
@Override
public int hashCode() {
// TODO Auto-generated method stub
int res = 17;
for(int i = 0; i < size; i++)
res = res*37+arr[i].hashCode();
return res;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return Arrays.toString(arr);
}
public static void main(String[] args) {
HashMap<Tuple,String> birthDay = new HashMap<Tuple,String>();
Tuple p = new Tuple(1,2,1986);
Tuple.setTypeLock(true);
Tuple p2 = new Tuple(2,10,2009);
Tuple p3 = new Tuple(1,2,2010);
Tuple p4 = new Tuple(1,2,2010);
birthDay.put(p,"Kevin");
birthDay.put(p2,"Smith");
birthDay.put(p3,"Sam");
birthDay.put(p4, "Jack");
System.out.println(birthDay);
System.out.println(birthDay.get(new Tuple(1,2,1986)));
birthDay.put(new Tuple(1,2,""),"");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
感谢边做边学。以下是改进的“机会”建议:
只有一种元组可以存在(一旦设置了类型锁)。这会损害想要使用多种类型元组的程序的可重用性和可扩展性,除非您采用剪切粘贴重用(BirthdayTuple、DimensionsTuple、StreetAddressTuple,...)。考虑一个 TupleFactory 类,它接受目标类型并创建一个元组构建器对象来生成元组。
“null”作为元组中的值的有效性没有记录。我认为在设置Typelock之前,允许null;但是设置 Typelock 后,代码将生成 NullPointerException - 这是不一致的。如果不允许,构造函数应该捕获它并禁止它(无论类型锁如何)。如果它们被允许,那么整个代码(构造函数、等于、哈希码等)需要修改以允许它。
决定元组是否旨在成为不可变值对象。基于它缺乏 setter 方法,我猜是这样的。如果是这样,那么请小心“采用”传入的数组 -
lastTuple=this.arr
。即使它是一个 var arg 构造函数,也可以直接使用数组调用该构造函数。该类采用该数组(保留对其的引用),并且数组中的值随后可以在类外部更改。我会做一个数组的浅拷贝,但也会记录具有非不可变值的元组的潜在问题(可以在元组外部更改)。您的
equals
方法缺少 null 检查(if (obj == null) return false
)和类检查(obj instanceof Tuple
> 或this.getClass().equals(object.getClass())
)。 equals 习惯用法已有详细记录。除了通过
toString
之外,没有其他方法可以查看元组的值。这保护了 的值和整体不变性,但我认为它限制了类的有用性。虽然我意识到这只是一个例子,但我不希望将此类用于生日/日期之类的事情。在具有固定对象类型的解决方案领域中,真实的类(如日期)要好得多。我想这个类在元组是第一类对象的特定领域很有用。
编辑
一直在想这个。这是我对一些代码的看法(关于 github + 测试):
Kudos on learning by doing. Here are suggestions of "opportunities" for improvement:
Only one kind of Tuple can ever exist (once Typelock is set). This hurts reusability and scalability in programs wanting to use multiple types of Tuples unless you resort to cut-n-paste reuse (BirthdayTuple, DimensionsTuple, StreetAddressTuple, ...). Consider a TupleFactory class that accepts the target types and creates a tuple builder object to generate tuples.
The validity of "null" as a value in a Tuple isn't documented. I think before Typelock is set, null is allowed; but after Typelock is set, code will generate a NullPointerException - this is inconsistent. If they are not allowed, the constructor should catch it and disallow it (regardless of Typelock). If they are allowed, then the code overall (constructor, equals, hashcode, etc) needs modification to allow for it.
Decide whether Tuples are intended to be immutable value objects. Based on its lack of setter methods, I'd guess so. If so, then be careful of "adopting" the incoming array -
lastTuple=this.arr
. Even though its a var arg constructor, the constructor could be called with an array directly. The class adopts the array (keeps a reference to it) and the values in the array could be altered outside the class afterward. I'd do a shallow copy of the array, but also document the potential issue with Tuples with non-immutable values (that could be changed outside the Tuple).Your
equals
method lacks the null check (if (obj == null) return false
) and the class check (eitherobj instanceof Tuple
orthis.getClass().equals(object.getClass())
). The equals idiom is well documented.There's no way to view the values of a Tuple except through
toString
. This protects the values and the overall immutability of , but I think it limits the usefulness of the class.While I realize its just an example, I wouldn't expect to use this class for something like birthdays/dates. In solution domains with fixed object types, real classes (like Date) are so much better. I would imagine this class to be useful in specific domains where tuples are first class objects.
Edit
Been thinking about this. Here's my take on some code (on github + tests):
这种类型如何安全?您将引发运行时异常,而不是在编译时报告类型错误。
您正在尝试对数量进行抽象,这在静态类型语言中(到目前为止)是不可能的,而不失去类型安全性。
附录:
元组可以由异构元素(即具有不同类型的元素)组成。因此,对于这个 Tuple 类,提供“rutime 类型安全”是不可能的。班级的客户负责进行适当的演员阵容。
这是您在 Java 中可以做的最好的事情:(编辑:请参阅Brent 的帖子,以更好地实现Tuple
。(它不需要在客户端进行类型转换。))How is this typesafe? You are throwing runtime exceptions instead of reporting type errors at compile time.
You are trying to abstract over arity which is (as of yet) not possible in statically typed languages, without losing typesafety.
Addendum:
Tuples can consist of heterogeneous elements (i.e. elements with different types). Therefore providing even "rutime typesafety" is not possible, for this
Tuple
class. Clients of the class are responsible for making the appropriate casts.This is the best you can do in Java :(Edit: See Brent's post for a better implementation ofTuple
. (It doesn't require typecasts on the client side.))这是最简单的解决方案,也是最好的解决方案。它类似于 .NET 中元组的表示方式。它小心翼翼地避开了java擦除。它是强类型的。它不会抛出异常。它非常容易使用。
This is the simplest solution and it's also the best. It's similar to how Tuples are represented in .NET. It carefully sidesteps java erasure. It is strongly typed. It does not throw exceptions. It is very easy to use.
您应该查看.NET 的 Tuple 实现。它们是编译时类型安全的。
You should look at .NET's Tuple's implementation. They are compile time type-safe.
typeLock
的用途是什么?允许某人阻止建造更多这些物体?这部分没有多大意义。为什么你想让别人阻止你的对象的进一步实例化?如果由于某种原因这是您所需要的,而不是“锁定”类并引发异常,只需确保代码路径...不会创建更多该类型的对象。
将静态
lastTuple
设置为最后实例化Tuple
的引用的目的是什么?像这样混合静态引用是一种糟糕的做法。坦率地说,代码相当混乱,尽管对这个类的需求很混乱。如果不知何故这是我在工作环境中审查的代码,我不会允许它。
What is the purpose of
typeLock
? To allow someone to prevent constructing any more of these objects? This part doesn't make much sense.Why would you ever want to let someone prevent further instantiation of your objects? If for some reason this is something you ever need, instead of "locking" a class and throwing exceptions, just make sure the code path ... doesn't create more objects of the type.
What's the purpose of the static
lastTuple
which is set to a reference of the last instantiatedTuple
? It's a poor practice to mix static references like this.Frankly the code is quite confusing, even though the need for this class is confusing. If somehow this was code I was reviewing in a work environment, I would not allow it.
为了编译时类型安全,最好使用泛型。您可以为每个数量定义一个接口。然后,您可以定义单独的 Callable 接口来访问元组的值。
It would be better to use generics for compile time type safety. You can define one interface per arity. Then you can define separate Callable interfaces to access the values of the tuple.
在wave项目中看到这段代码
saw this code in wave project
这是一个非常糟糕的 n 元组实现,它使用泛型来提供编译时类型检查。主要方法(出于演示目的而提供)展示了使用它是多么可怕:
Here's a truly awful n-tuple implementation that uses generics to provide compile-time type checks. The main method (provided for demo purposes) shows just how horrendous this would be to use:
如果您真的对编写类型安全容器感兴趣,请研究泛型:
If you're really interested in writing type-safe containers, look into generics: