Skip to content

Commit f0c0807

Browse files
committed
Add LinQ helper class
1 parent 3c7e859 commit f0c0807

File tree

3 files changed

+139
-0
lines changed

3 files changed

+139
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.acidmanic.pactdoc.dcoumentstructure.namextractors;
7+
8+
import com.acidmanic.pact.helpers.NameExtractor;
9+
import com.acidmanic.pact.helpers.Normalizer;
10+
import com.acidmanic.pact.helpers.RequestPathBuilder;
11+
import com.acidmanic.pactmodels.Interaction;
12+
import com.acidmanic.pactmodels.Request;
13+
14+
/**
15+
*
16+
* @author diego
17+
*/
18+
public class EndpointNameExtractor implements NameExtractor<Interaction> ,Normalizer<String>{
19+
20+
@Override
21+
public String extract(Interaction sourceData) {
22+
23+
Request request = sourceData.getRequest();
24+
25+
if (request != null && request.getPath() != null) {
26+
27+
String pathString = request.getPath();
28+
29+
String endPointUri = new RequestPathBuilder().stripParameters(pathString);
30+
31+
return endPointUri;
32+
}
33+
return "";
34+
}
35+
36+
@Override
37+
public String normalize(String value) {
38+
39+
return value;
40+
}
41+
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.acidmanic.pactdoc.dcoumentstructure.namextractors;
7+
8+
import com.acidmanic.pact.helpers.NameExtractor;
9+
import com.acidmanic.pact.helpers.Normalizer;
10+
import com.acidmanic.pact.models.EndPoint;
11+
import com.acidmanic.pactmodels.Interaction;
12+
import com.acidmanic.pactmodels.Request;
13+
14+
/**
15+
*
16+
* @author diego
17+
*/
18+
public class ServiceNameExtractor implements NameExtractor<EndPoint>, Normalizer<String> {
19+
20+
@Override
21+
public String extract(EndPoint sourceData) {
22+
23+
if (sourceData.getInteractions() != null && !sourceData.getInteractions().isEmpty()) {
24+
25+
Interaction interaction = sourceData.getInteractions().get(0);
26+
27+
if (interaction != null && interaction.getRequest() != null) {
28+
29+
Request request = interaction.getRequest();
30+
31+
String path = request.getPath();
32+
33+
if (path != null && path.length() > 0) {
34+
35+
String[] uriSegments = path.split("/");
36+
37+
if (uriSegments.length > 0) {
38+
39+
return uriSegments[0];
40+
}
41+
}
42+
}
43+
}
44+
return "";
45+
}
46+
47+
@Override
48+
public String normalize(String value) {
49+
50+
return value;
51+
}
52+
53+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
package com.acidmanic.pactdoc.utility;
7+
8+
import com.acidmanic.delegates.arg1.Function;
9+
import java.util.ArrayList;
10+
import java.util.Collection;
11+
import java.util.List;
12+
13+
/**
14+
*
15+
* @author diego
16+
*/
17+
public class LinQ {
18+
19+
public static <TObject, TProperty> List<TProperty> select(Collection<TObject> collection, Function<TProperty, TObject> selector) {
20+
21+
List<TProperty> result = new ArrayList<>();
22+
23+
for (TObject item : collection) {
24+
25+
TProperty value = selector.perform(item);
26+
27+
result.add(value);
28+
}
29+
30+
return result;
31+
}
32+
33+
public static <T> T first(Collection<T> collection, Function<Boolean, T> expression) {
34+
35+
for (T item : collection) {
36+
37+
if (expression.perform(item)) {
38+
39+
return item;
40+
}
41+
}
42+
return null;
43+
}
44+
}

0 commit comments

Comments
 (0)