Note that there are some explanatory texts on larger screens.

plurals
  1. POJava N-Tuple implementation
    text
    copied!<p>I just made a Java n-tuple which is type-safe.<br> I'm using some unconventional methods to achieve type-safety (I just made it for fun).</p> <p>Can someone can give some input on improving it or some possible flaws.</p> <pre><code>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&lt;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 &lt; 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 &lt; 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&lt;Tuple,String&gt; birthDay = new HashMap&lt;Tuple,String&gt;(); 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,""),""); } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload