Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit cf96bc7

Browse filesBrowse files
committed
Add LINQ Conversions
1 parent d2277b1 commit cf96bc7
Copy full SHA for cf96bc7

File tree

Expand file treeCollapse file tree

3 files changed

+118
-4
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+118
-4
lines changed

‎src/app/src/main/java/servicestack/net/javalinqexamples/Conversion.java

Copy file name to clipboardExpand all lines: src/app/src/main/java/servicestack/net/javalinqexamples/Conversion.java
+56Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22

33
import com.android.internal.util.Predicate;
44

5+
import java.io.Console;
6+
import java.util.Arrays;
57
import java.util.Date;
68
import java.util.List;
9+
import java.util.Map;
710

811
import servicestack.net.javalinqexamples.support.Customer;
912
import servicestack.net.javalinqexamples.support.Func;
@@ -19,4 +22,57 @@
1922
* Created by mythz on 7/26/2015.
2023
*/
2124
public class Conversion {
25+
26+
public void linq54(){
27+
double[] doubles = new double[] { 1.7, 2.3, 1.9, 4.1, 2.9 };
28+
29+
List<Double> sortedDoubles = orderByDesc(toList(doubles));
30+
31+
Double[] doublesArray = toArray(sortedDoubles, Double.class);
32+
33+
Log.d("Every other double from highest to lowest:");
34+
for (int d = 0; d < doublesArray.length; d += 2){
35+
Log.d(doublesArray[d]);
36+
}
37+
}
38+
39+
public void linq55(){
40+
String[] words = new String[] { "cherry", "apple", "blueberry" };
41+
42+
List<String> sortedWords = orderBy(words);
43+
List<String> wordList = toList(sortedWords);
44+
45+
Log.d("The sorted word list:");
46+
for (String w : wordList){
47+
Log.d(w);
48+
}
49+
}
50+
51+
public void linq56(){
52+
List<Tuple<String,Integer>> scoreRecords = toList(
53+
new Tuple<>("Alice", 50),
54+
new Tuple<>("Bob", 40),
55+
new Tuple<>("Cathy", 45)
56+
);
57+
58+
Map<String,Tuple<String,Integer>> scoreRecordsDict = toDictionary(scoreRecords, new Function<Tuple<String, Integer>, String>() {
59+
@Override
60+
public String apply(Tuple<String, Integer> t) {
61+
return t.A;
62+
}
63+
});
64+
65+
Log.d("Bob's score: " + scoreRecordsDict.get("Bob"));
66+
}
67+
68+
public void linq57(){
69+
Object[] numbers = new Object[] { null, 1.0, "two", 3, "four", 5, "six", 7.0 };
70+
71+
List<Double> doubles = ofType(toList(numbers), Double.class);
72+
73+
Log.d("Numbers stored as doubles:");
74+
for (Double d : doubles){
75+
Log.d(d);
76+
}
77+
}
2278
}

‎src/app/src/main/java/servicestack/net/javalinqexamples/MainActivity.java

Copy file name to clipboardExpand all lines: src/app/src/main/java/servicestack/net/javalinqexamples/MainActivity.java
+4-4Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ protected void onCreate(Bundle savedInstanceState) {
4949
// Run(new Partitioning());
5050
// Run(new Ordering());
5151
// Run(new Grouping());
52-
Run(new SetOperators());
52+
// Run(new SetOperators());
53+
Run(new Conversion());
5354
}
5455

5556
void Run(Object linqExamples){
@@ -62,9 +63,8 @@ void Run(Object linqExamples){
6263
Log.i("\n" + method.getName().toUpperCase() + ":");
6364
try {
6465
method.invoke(linqExamples);
65-
} catch (IllegalAccessException e) {
66-
e.printStackTrace();
67-
} catch (InvocationTargetException e) {
66+
} catch (IllegalAccessException | InvocationTargetException e) {
67+
Log.e(e.getCause().toString());
6868
e.printStackTrace();
6969
}
7070
}

‎src/app/src/main/java/servicestack/net/javalinqexamples/support/Func.java

Copy file name to clipboardExpand all lines: src/app/src/main/java/servicestack/net/javalinqexamples/support/Func.java
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import com.android.internal.util.Predicate;
44

5+
import java.lang.reflect.Array;
56
import java.util.ArrayList;
67
import java.util.Arrays;
8+
import java.util.Collection;
79
import java.util.Collections;
810
import java.util.Comparator;
911
import java.util.HashMap;
@@ -121,6 +123,7 @@ public static ArrayList<Double> toList(double... xs){
121123
return to;
122124
}
123125

126+
@SafeVarargs
124127
public static <T> ArrayList<T> toList(T... xs){
125128
ArrayList<T> to = new ArrayList<>();
126129
for (T x : xs) {
@@ -139,6 +142,61 @@ public static <T> ArrayList<T> toList(Iterable<T> xs){
139142
return to;
140143
}
141144

145+
public static <T> T[] toArray(Iterable<T> xs, Class<T> cls) { return toArray(toList(xs), cls);}
146+
147+
@SuppressWarnings("unchecked")
148+
public static <T> T[] toArray(List<T> list, Class<T> cls) {
149+
T[] array = (T[])Array.newInstance(cls, list.size());
150+
return list.toArray(array);
151+
}
152+
153+
public static <K,V> HashMap<K,V> toDictionary(K k1, V v1) {
154+
HashMap<K,V> to = new HashMap<>();
155+
to.put(k1, v1);
156+
return to;
157+
}
158+
159+
public static <K,V> HashMap<K,V> toDictionary(K k1, V v1, K k2, V v2) {
160+
HashMap<K,V> to = new HashMap<>();
161+
to.put(k1, v1);
162+
to.put(k2, v2);
163+
return to;
164+
}
165+
166+
public static <K,V> HashMap<K,V> toDictionary(K k1, V v1, K k2, V v2, K k3, V v3) {
167+
HashMap<K,V> to = new HashMap<>();
168+
to.put(k1, v1);
169+
to.put(k2, v2);
170+
to.put(k3, v3);
171+
return to;
172+
}
173+
174+
public static <K,V> HashMap<K,V> toDictionary(Tuple<K,V>... xs) {
175+
HashMap<K,V> to = new HashMap<>();
176+
for (Tuple<K,V> x : xs){
177+
to.put(x.A, x.B);
178+
}
179+
return to;
180+
}
181+
182+
public static <K, T> HashMap<K,T> toDictionary(Iterable<T> xs, Function<T,K> f) {
183+
HashMap<K,T> to = new HashMap<>();
184+
for (T x : xs){
185+
K key = f.apply(x);
186+
to.put(key, x);
187+
}
188+
return to;
189+
}
190+
191+
public static <T> ArrayList<T> ofType(Iterable xs, Class<T> cls){
192+
ArrayList<T> to = new ArrayList<T>();
193+
for (Object x : xs){
194+
if (cls.isInstance(x))
195+
to.add((T)x);
196+
}
197+
return to;
198+
}
199+
142200
public static <T,R> ArrayList<R> map(T[] xs, Function<T,R> f) { return map(toList(xs), f); }
143201

144202
public static <T,R> ArrayList<R> map(Iterable<T> xs, Function<T,R> f) {

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.