Skip to content

Commit fedea6b

Browse files
committed
Added conversions for fj.data to standard java.util classes
1 parent aa02710 commit fedea6b

File tree

8 files changed

+85
-1
lines changed

8 files changed

+85
-1
lines changed

core/src/main/java/fj/data/Array.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,10 @@ public Object[] array() {
127127
return copyOf(a, a.length);
128128
}
129129

130+
public A[] toJavaArray() {
131+
return (A[]) array();
132+
}
133+
130134
/**
131135
* Returns an option projection of this array; <code>None</code> if empty, or the first element in
132136
* <code>Some</code>.
@@ -795,6 +799,13 @@ public int size() {
795799
};
796800
}
797801

802+
/**
803+
* Returns a standard java.util.List projection of this array.
804+
*/
805+
java.util.List<A> toJavaList() {
806+
return new ArrayList<A>(toCollection());
807+
}
808+
798809
/**
799810
* Takes the given iterable to an array.
800811
*

core/src/main/java/fj/data/DList.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,13 @@ public static <A> DList<A> arrayDList(final A...as) {
7171
public List<A> run() {
7272
return appendFn.f(List.<A>nil()).run();
7373
}
74+
75+
/**
76+
* Converts the DList to a standard java.util.List.
77+
*/
78+
public java.util.List toJavaList() {
79+
return run().toJavaList();
80+
}
7481

7582
/**
7683
* A empty DList.

core/src/main/java/fj/data/HashMap.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import java.util.Collection;
77
import java.util.Iterator;
8+
import java.util.Map;
89

910
import static fj.P.p;
1011
import static fj.data.Option.fromNull;
@@ -341,6 +342,18 @@ public static <K, V> HashMap<K, V> from(final Iterable<P2<K, V>> entries) {
341342
return iterableHashMap(Equal.<K>anyEqual(), Hash.<K>anyHash(), entries);
342343
}
343344

345+
public static <K, V> HashMap<K, V> fromMap(java.util.Map<K, V> map) {
346+
return fromMap(Equal.anyEqual(), Hash.anyHash(), map);
347+
}
348+
349+
public static <K, V> HashMap<K, V> fromMap(Equal<K> eq, Hash<K> h, java.util.Map<K, V> map) {
350+
HashMap<K, V> m = HashMap.hashMap(eq, h);
351+
for (Map.Entry<K, V> e: map.entrySet()) {
352+
m.set(e.getKey(), e.getValue());
353+
}
354+
return m;
355+
}
356+
344357
/**
345358
* Converts the Iterable to a HashMap
346359
*

core/src/main/java/fj/data/HashSet.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,18 @@ public List<A> toList() {
225225
return m.keys();
226226
}
227227

228+
public java.util.List<A> toJavaList() {
229+
return toList().toJavaList();
230+
}
231+
232+
public java.util.Set<A> toJavaSet() {
233+
return new java.util.HashSet<A>(toCollection());
234+
}
235+
236+
public static <A> HashSet<A> fromSet(java.util.Set<A> s) {
237+
return iterableHashSet(s);
238+
}
239+
228240
/**
229241
* Projects an immutable collection of this hash set.
230242
*

core/src/main/java/fj/data/List.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,15 +197,22 @@ public final Stream<A> toStream() {
197197
*/
198198
@SuppressWarnings({"unchecked"})
199199
public final Array<A> toArray() {
200+
return mkArray(toArrayObject());
201+
}
202+
203+
public final Object[] toArrayObject() {
200204
final int length = length();
201205
final Object[] a = new Object[length];
202206
List<A> x = this;
203207
for (int i = 0; i < length; i++) {
204208
a[i] = x.head();
205209
x = x.tail();
206210
}
211+
return a;
212+
}
207213

208-
return mkArray(a);
214+
public final A[] toJavaArray() {
215+
return (A[]) toArrayObject();
209216
}
210217

211218
/**

core/src/main/java/fj/data/Stream.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -916,6 +916,17 @@ public final Option<A> toOption() {
916916
return isEmpty() ? Option.<A>none() : some(head());
917917
}
918918

919+
@SuppressWarnings({"unchecked", "UnnecessaryFullyQualifiedName"})
920+
public final A[] toJavaArray() {
921+
final A[] array = (A[]) new Object[length()];
922+
int i = 0;
923+
for (A a: this) {
924+
array[i] = a;
925+
i++;
926+
}
927+
return array;
928+
}
929+
919930
/**
920931
* Returns a list projection of this stream.
921932
*
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package fj.data;
2+
3+
/**
4+
* Created by MarkPerry on 14 Feb 16.
5+
*/
6+
public class ArrayTest {
7+
8+
9+
public void test1() {
10+
11+
}
12+
13+
}

core/src/test/java/fj/data/ListTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,14 @@ public void listReduce() {
8181
assertThat(expected, equalTo(list));
8282
}
8383

84+
@Test
85+
public void toJavaArray() {
86+
final int max = 3;
87+
Integer[] ints = new Integer[max];
88+
for (int i = 0; i < max; i++) {
89+
ints[i] = i + 1;
90+
};
91+
assertThat(List.range(1, max + 1).toJavaArray(), equalTo(ints));
92+
}
93+
8494
}

0 commit comments

Comments
 (0)