Mercurial > hg > truffle
comparison truffle/com.oracle.truffle.tck/src/com/oracle/truffle/tck/ComplexNumber.java @ 22495:aeba89e1d8da
Add ComplexNumber sequence tests
author | Matthias Grimmer <grimmer@ssw.jku.at> |
---|---|
date | Fri, 11 Dec 2015 15:20:27 +0100 |
parents | fa7b15454c66 |
children | b3569a53c24c |
comparison
equal
deleted
inserted
replaced
22489:28227895fa35 | 22495:aeba89e1d8da |
---|---|
31 import com.oracle.truffle.api.interop.ForeignAccess.Factory; | 31 import com.oracle.truffle.api.interop.ForeignAccess.Factory; |
32 import com.oracle.truffle.api.interop.Message; | 32 import com.oracle.truffle.api.interop.Message; |
33 import com.oracle.truffle.api.interop.TruffleObject; | 33 import com.oracle.truffle.api.interop.TruffleObject; |
34 import com.oracle.truffle.api.nodes.RootNode; | 34 import com.oracle.truffle.api.nodes.RootNode; |
35 | 35 |
36 public class ComplexNumber implements TruffleObject { | 36 public final class ComplexNumber implements TruffleObject { |
37 | 37 |
38 public static final String REAL_IDENTIFIER = "real"; | 38 public static final String REAL_IDENTIFIER = "real"; |
39 public static final String IMAGINARY_IDENTIFIER = "imaginary"; | 39 public static final String IMAGINARY_IDENTIFIER = "imaginary"; |
40 | 40 |
41 private final double[] data = new double[2]; | 41 private double real; |
42 private double imag; | |
42 | 43 |
43 public ComplexNumber(double real, double imaginary) { | 44 public ComplexNumber(double real, double imaginary) { |
44 data[0] = real; | 45 this.real = real; |
45 data[1] = imaginary; | 46 this.imag = imaginary; |
46 } | 47 } |
47 | 48 |
48 public ForeignAccess getForeignAccess() { | 49 public ForeignAccess getForeignAccess() { |
49 return ForeignAccess.create(new ComplexForeignAccessFactory()); | 50 return ForeignAccess.create(new ComplexForeignAccessFactory()); |
50 } | 51 } |
51 | 52 |
52 private static int identifierToIndex(String identifier) { | 53 public void set(String identifier, double value) { |
53 switch (identifier) { | 54 switch (identifier) { |
54 case REAL_IDENTIFIER: | 55 case REAL_IDENTIFIER: |
55 return 0; | 56 this.real = value; |
57 break; | |
56 case IMAGINARY_IDENTIFIER: | 58 case IMAGINARY_IDENTIFIER: |
57 return 1; | 59 this.imag = value; |
60 break; | |
58 default: | 61 default: |
59 throw new IllegalArgumentException(); | 62 throw new IllegalArgumentException(); |
60 } | 63 } |
61 } | 64 } |
62 | 65 |
63 public void set(String identifier, double value) { | |
64 data[identifierToIndex(identifier)] = value; | |
65 } | |
66 | |
67 public double get(String identifier) { | 66 public double get(String identifier) { |
68 return data[identifierToIndex(identifier)]; | 67 switch (identifier) { |
68 case REAL_IDENTIFIER: | |
69 return this.real; | |
70 case IMAGINARY_IDENTIFIER: | |
71 return this.imag; | |
72 default: | |
73 throw new IllegalArgumentException(); | |
74 } | |
69 } | 75 } |
70 | 76 |
71 private static class ComplexForeignAccessFactory implements Factory { | 77 private static class ComplexForeignAccessFactory implements Factory { |
72 | 78 |
73 public boolean canHandle(TruffleObject obj) { | 79 public boolean canHandle(TruffleObject obj) { |