-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathNextMarketAPI.java
More file actions
136 lines (120 loc) · 4.19 KB
/
NextMarketAPI.java
File metadata and controls
136 lines (120 loc) · 4.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.nextplugins.nextmarket.api;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Inject;
import com.nextplugins.nextmarket.api.model.category.Category;
import com.nextplugins.nextmarket.api.model.product.Product;
import com.nextplugins.nextmarket.manager.CategoryManager;
import com.nextplugins.nextmarket.storage.ProductStorage;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.function.Predicate;
import java.util.stream.Collectors;
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class NextMarketAPI {
@Getter public static final NextMarketAPI instance = new NextMarketAPI();
@Inject private CategoryManager categoryManager;
@Inject private ProductStorage productStorage;
/**
* Search all categories to look for one with the entered id.
*
* @param id category id to search
* @return {@link Optional} with the category found
*/
public Optional<Category> findCategoryById(String id) {
return categoryManager.findCategoryById(id);
}
/**
* Search all categories to look for one with the entered custom filter.
*
* @param filter custom filter to search
* @return {@link Optional} with the category found
*/
public Optional<Category> findCategoryByFilter(Predicate<Category> filter) {
return allCategories().stream()
.filter(filter)
.findFirst();
}
/**
* Search all categories to look for every with the entered custom filter.
*
* @param filter custom filter to search
* @return {@link Set} with all categories found
*/
public Set<Category> findCategoriesByFilter(Predicate<Category> filter) {
return allCategories().stream()
.filter(filter)
.collect(Collectors.toSet());
}
/**
* Retrieve all products from the category.
*
* @param category category to search products
* @return {@link Set} with all products found
*/
public Set<Product> findProductsByCategory(Category category) {
return productStorage.findProductsByCategory(category);
}
/**
* Retrieve all products from the seller.
*
* @param uniqueId products seller {@link UUID}
* @return {@link Set} with all products found
*/
public Set<Product> findProductsBySeller(UUID uniqueId) {
return productStorage.findProductsBySeller(uniqueId);
}
/**
* Retrieve all products to destination player.
*
* @param uniqueId products destination {@link UUID}
* @return {@link Set} with all products found
*/
public Set<Product> findProductsByDestination(UUID uniqueId) {
return productStorage.findProductsByDestination(uniqueId);
}
/**
* Search all products to look for one with the entered custom filter.
*
* @param filter custom filter to search
* @return {@link Optional} with the product found
*/
public Optional<Product> findProductByFilter(Predicate<Product> filter) {
return allProducts().stream()
.filter(filter)
.findFirst();
}
/**
* Search all products to look for every with the entered custom filter.
*
* @param filter custom filter to search
* @return {@link Set} with all products found
*/
public Set<Product> findProductsByFilter(Predicate<Product> filter) {
return allProducts().stream()
.filter(filter)
.collect(Collectors.toSet());
}
/**
* Retrieve all categories registered in configuration.
*
* @return {@link Set} with categories
*/
public Set<Category> allCategories() {
return ImmutableSet.copyOf(categoryManager.getCategoryMap().values());
}
/**
* Retrieve all products loaded so far.
*
* @return {@link Set} with products
*/
public Set<Product> allProducts() {
Set<Product> products = new LinkedHashSet<>();
productStorage.getProducts().values().forEach(products::addAll);
return ImmutableSet.copyOf(products);
}
}