forked from digital-asset/daml
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
java representation for value GenMap (digital-asset#3515)
- Loading branch information
1 parent
3d960c7
commit 027d63a
Showing
11 changed files
with
244 additions
and
22 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
language-support/java/bindings/src/main/java/com/daml/ledger/javaapi/data/DamlGenMap.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
// Copyright (c) 2019 The DAML Authors. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.daml.ledger.javaapi.data; | ||
|
||
import com.digitalasset.ledger.api.v1.ValueOuterClass; | ||
import org.checkerframework.checker.nullness.qual.NonNull; | ||
|
||
import javax.annotation.Nonnull; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.StringJoiner; | ||
|
||
public class DamlGenMap extends Value { | ||
|
||
private static DamlGenMap EMPTY = new DamlGenMap(Collections.EMPTY_MAP); | ||
|
||
private final java.util.Map<Value, Value> value; | ||
|
||
public DamlGenMap(java.util.Map<Value, Value> value) { | ||
this.value = value; | ||
} | ||
|
||
public @Nonnull | ||
java.util.Map<Value, Value> getMap() { return value; } | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
DamlGenMap optional = (DamlGenMap) o; | ||
return Objects.equals(value, optional.value); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return value.hashCode(); | ||
} | ||
|
||
@Override | ||
public @NonNull String toString() { | ||
StringJoiner sj = new StringJoiner(", ", "GenMap{", "}"); | ||
value.forEach((key, value) -> sj.add(key.toString()+ " -> " + value.toString())); | ||
return sj.toString(); | ||
} | ||
|
||
@Override | ||
public @Nonnull ValueOuterClass.Value toProto() { | ||
ValueOuterClass.GenMap.Builder mb = ValueOuterClass.GenMap.newBuilder(); | ||
value.forEach((key, value) -> | ||
mb.addEntries(ValueOuterClass.GenMap.Entry.newBuilder() | ||
.setKey(key.toProto()) | ||
.setValue(value.toProto()) | ||
)); | ||
return ValueOuterClass.Value.newBuilder().setGenMap(mb).build(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
language-support/java/codegen/src/test/java/com/digitalasset/testing/AllTestsFor1_dev.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) 2019 The DAML Authors. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.digitalasset.testing; | ||
|
||
import org.junit.runner.RunWith; | ||
import org.junit.runners.Suite; | ||
|
||
import java.math.BigDecimal; | ||
|
||
@RunWith(Suite.class) | ||
@Suite.SuiteClasses({ | ||
DecimalTestForAll.class, | ||
NumericTestFor1_7AndFor1_dev.class, | ||
GenMapTestFor1_dev.class, | ||
}) | ||
public class AllTestsFor1_dev { } |
67 changes: 67 additions & 0 deletions
67
language-support/java/codegen/src/test/java/com/digitalasset/testing/GenMapTestFor1_dev.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
// Copyright (c) 2019 The DAML Authors. All rights reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package com.digitalasset.testing; | ||
|
||
|
||
import com.daml.ledger.javaapi.data.*; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.platform.runner.JUnitPlatform; | ||
import org.junit.runner.RunWith; | ||
import test.genmapmod.Box; | ||
import test.recordmod.Pair; | ||
import test.variantmod.Either; | ||
import test.variantmod.either.*; | ||
import java.math.BigDecimal; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
@RunWith(JUnitPlatform.class) | ||
public class GenMapTestFor1_dev { | ||
|
||
private BigDecimal bg1 = new BigDecimal("1.0000000000"); | ||
private BigDecimal bg2 = new BigDecimal("-2.2222222222"); | ||
private BigDecimal bg3 = new BigDecimal("3.3333333333"); | ||
|
||
@Test | ||
void genMap2Value2GenMap() { | ||
HashMap<Pair<Long, BigDecimal>, Either<Long, BigDecimal>> map = new HashMap<>(); | ||
map.put(new Pair<>(1L, bg1), new Right<>(bg1)); | ||
map.put(new Pair<>(2L, bg2), new Left<>(2L)); | ||
map.put(new Pair<>(3L, bg3), new Right<>(bg3)); | ||
Box b = new Box(map, "alice"); | ||
assertEquals(Box.fromValue(b.toValue()), b); | ||
} | ||
|
||
private Record pair(Long fst, BigDecimal snd) { | ||
return new Record( | ||
new Record.Field("fst", new Int64(fst)), | ||
new Record.Field("snd", new Numeric(snd)) | ||
); | ||
} | ||
|
||
private Variant left(Long l) { | ||
return new Variant("Left", new Int64(l)); | ||
} | ||
|
||
private Variant right(BigDecimal r) { | ||
return new Variant("Right", new Numeric(r)); | ||
} | ||
|
||
@Test | ||
void value2GenMap2value() { | ||
Map<Value, Value> value = new HashMap<Value, Value>(); | ||
value.put(pair(1L, bg1), left(1L)); | ||
value.put(pair(-2L,bg2 ), right(bg2)); | ||
value.put(pair(3L, bg3), left(3L)); | ||
Value map = new DamlGenMap(value); | ||
Record b = new Record( | ||
new Record.Field("x", map), | ||
new Record.Field("party", new Party("alice")) | ||
); | ||
assertEquals(Box.fromValue(b).toValue(), b); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters