Skip to content

Commit cf08c31

Browse files
author
Sylvain Lebresne
committed
Make collections returned by Row immutable.
JAVA-205 #fixes
1 parent ede4cbd commit cf08c31

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

driver-core/CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ CHANGELOG
55
------
66

77
- [new] OSGi bundle (JAVA-142)
8+
- [improvement] Make collections returned by Row immutable (JAVA-205)
89
- [bug] Don't retain unused PreparedStatement in memory (JAVA-201)
910
- [bug] Add missing clustering order info in TableMetadata
1011

driver-core/src/main/java/com/datastax/driver/core/Row.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,7 @@ public InetAddress getInet(String name) {
567567
* @return the value of the {@code i}th column in this row as a list of
568568
* {@code elementsClass} objects. If the value is NULL, an empty list is
569569
* returned (note that Cassandra makes no difference between an empty list
570-
* and column of type list that is not set).
570+
* and column of type list that is not set). The returned list is immutable.
571571
*
572572
* @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
573573
* @throws InvalidTypeException if column {@code i} is not a list or if its
@@ -588,7 +588,7 @@ public <T> List<T> getList(int i, Class<T> elementsClass) {
588588
return Collections.<T>emptyList();
589589

590590
// TODO: we could avoid the getCodec call if we kept a reference to the original message.
591-
return Codec.<List<T>>getCodec(type).compose(value);
591+
return Collections.unmodifiableList(Codec.<List<T>>getCodec(type).compose(value));
592592
}
593593

594594
/**
@@ -599,7 +599,7 @@ public <T> List<T> getList(int i, Class<T> elementsClass) {
599599
* @return the value of the {@code i}th column in this row as a list of
600600
* {@code elementsClass} objects. If the value is NULL, an empty list is
601601
* returned (note that Cassandra makes no difference between an empty list
602-
* and column of type list that is not set).
602+
* and column of type list that is not set). The returned list is immutable.
603603
*
604604
* @throws IllegalArgumentException if {@code name} is not part of the
605605
* ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}.
@@ -618,7 +618,7 @@ public <T> List<T> getList(String name, Class<T> elementsClass) {
618618
* @return the value of the {@code i}th column in this row as a set of
619619
* {@code elementsClass} objects. If the value is NULL, an empty set is
620620
* returned (note that Cassandra makes no difference between an empty set
621-
* and column of type set that is not set).
621+
* and column of type set that is not set). The returned set is immutable.
622622
*
623623
* @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
624624
* @throws InvalidTypeException if column {@code i} is not a set or if its
@@ -638,7 +638,7 @@ public <T> Set<T> getSet(int i, Class<T> elementsClass) {
638638
if (value == null)
639639
return Collections.<T>emptySet();
640640

641-
return Codec.<Set<T>>getCodec(type).compose(value);
641+
return Collections.unmodifiableSet(Codec.<Set<T>>getCodec(type).compose(value));
642642
}
643643

644644
/**
@@ -649,7 +649,7 @@ public <T> Set<T> getSet(int i, Class<T> elementsClass) {
649649
* @return the value of the {@code i}th column in this row as a set of
650650
* {@code elementsClass} objects. If the value is NULL, an empty set is
651651
* returned (note that Cassandra makes no difference between an empty set
652-
* and column of type set that is not set).
652+
* and column of type set that is not set). The returned set is immutable.
653653
*
654654
* @throws IllegalArgumentException if {@code name} is not part of the
655655
* ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}.
@@ -669,7 +669,8 @@ public <T> Set<T> getSet(String name, Class<T> elementsClass) {
669669
* @return the value of the {@code i}th column in this row as a map of
670670
* {@code keysClass} to {@code valuesClass} objects. If the value is NULL,
671671
* an empty map is returned (note that Cassandra makes no difference
672-
* between an empty map and column of type map that is not set).
672+
* between an empty map and column of type map that is not set). The
673+
* returned map is immutable.
673674
*
674675
* @throws IndexOutOfBoundsException if {@code i < 0 || i >= this.columns().size()}.
675676
* @throws InvalidTypeException if column {@code i} is not a map, if its
@@ -691,7 +692,7 @@ public <K, V> Map<K, V> getMap(int i, Class<K> keysClass, Class<V> valuesClass)
691692
if (value == null)
692693
return Collections.<K, V>emptyMap();
693694

694-
return Codec.<Map<K, V>>getCodec(type).compose(value);
695+
return Collections.unmodifiableMap(Codec.<Map<K, V>>getCodec(type).compose(value));
695696
}
696697

697698
/**
@@ -703,7 +704,8 @@ public <K, V> Map<K, V> getMap(int i, Class<K> keysClass, Class<V> valuesClass)
703704
* @return the value of the {@code i}th column in this row as a map of
704705
* {@code keysClass} to {@code valuesClass} objects. If the value is NULL,
705706
* an empty map is returned (note that Cassandra makes no difference
706-
* between an empty map and column of type map that is not set).
707+
* between an empty map and column of type map that is not set). The
708+
* returned map is immutable.
707709
*
708710
* @throws IllegalArgumentException if {@code name} is not part of the
709711
* ResultSet this row is part of, i.e. if {@code !this.columns().names().contains(name)}.

0 commit comments

Comments
 (0)