• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

CyclopsMC / CyclopsCore / #479033805

29 Aug 2025 03:01PM UTC coverage: 22.271% (+0.2%) from 22.05%
#479033805

push

github

rubensworks
Add IngredientComponentStorageComposite

31 of 36 new or added lines in 1 file covered. (86.11%)

2328 of 10453 relevant lines covered (22.27%)

0.22 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.11
/src/main/java/org/cyclops/cyclopscore/ingredient/storage/IngredientComponentStorageComposite.java
1
package org.cyclops.cyclopscore.ingredient.storage;
2

3
import com.google.common.collect.Iterators;
4
import org.cyclops.commoncapabilities.api.ingredient.IIngredientMatcher;
5
import org.cyclops.commoncapabilities.api.ingredient.IngredientComponent;
6
import org.cyclops.commoncapabilities.api.ingredient.storage.IIngredientComponentStorage;
7
import org.jetbrains.annotations.NotNull;
8

9
import java.util.Collection;
10
import java.util.Iterator;
11

12
/**
13
 * A composite ingredient component storage
14
 * @param <T> The instance type.
15
 * @param <M> The matching condition parameter, may be Void. Instances MUST properly implement the equals method.
16
 * @author rubensworks
17
 */
18
public class IngredientComponentStorageComposite<T, M> implements IIngredientComponentStorage<T, M> {
19

20
    protected final IngredientComponent<T, M> ingredientComponent;
21
    protected final Collection<IIngredientComponentStorage<T, M>> storages;
22

23
    public IngredientComponentStorageComposite(IngredientComponent<T, M> ingredientComponent, Collection<IIngredientComponentStorage<T, M>> storages) {
1✔
24
        this.ingredientComponent = ingredientComponent;
1✔
25
        this.storages = storages;
1✔
26
    }
1✔
27

28
    @Override
29
    public IngredientComponent<T, M> getComponent() {
30
        return this.ingredientComponent;
1✔
31
    }
32

33
    @Override
34
    public Iterator<T> iterator() {
35
        return Iterators.concat(Iterators.transform(this.storages.iterator(), IIngredientComponentStorage::iterator));
1✔
36
    }
37

38
    @Override
39
    public Iterator<T> iterator(@NotNull T prototype, M matchCondition) {
40
        return Iterators.concat(Iterators.transform(this.storages.iterator(), i -> i.iterator(prototype, matchCondition)));
1✔
41
    }
42

43
    @Override
44
    public long getMaxQuantity() {
45
        long sum = 0;
1✔
46
        Iterator<IIngredientComponentStorage<T, M>> it = this.storages.iterator();
1✔
47
        while (it.hasNext() && sum < Long.MAX_VALUE) {
1✔
48
            try {
49
                sum = Math.addExact(sum, it.next().getMaxQuantity());
1✔
NEW
50
            } catch (ArithmeticException e) {
×
NEW
51
                sum = Long.MAX_VALUE; // If we had an overflow, we're already at max quantity.
×
52
            }
1✔
53
        }
54
        return sum;
1✔
55
    }
56

57
    @Override
58
    public T insert(@NotNull T ingredient, boolean simulate) {
59
        IIngredientMatcher<T, M> matcher = getComponent().getMatcher();
1✔
60
        for (IIngredientComponentStorage<T, M> storage : this.storages) {
1✔
61
            ingredient = storage.insert(ingredient, simulate);
1✔
62
            if (matcher.isEmpty(ingredient)) {
1✔
63
                break;
1✔
64
            }
NEW
65
        }
×
66
        return ingredient;
1✔
67
    }
68

69
    @Override
70
    public T extract(@NotNull T prototype, M matchCondition, boolean simulate) {
71
        IIngredientMatcher<T, M> matcher = getComponent().getMatcher();
1✔
72
        for (IIngredientComponentStorage<T, M> storage : this.storages) {
1✔
73
            T extracted = storage.extract(prototype, matchCondition, simulate);
1✔
74
            if (!matcher.isEmpty(extracted)) {
1✔
75
                return extracted;
1✔
76
            }
77
        }
1✔
78
        return matcher.getEmptyInstance();
1✔
79
    }
80

81
    @Override
82
    public T extract(long maxQuantity, boolean simulate) {
83
        IIngredientMatcher<T, M> matcher = getComponent().getMatcher();
1✔
84
        for (IIngredientComponentStorage<T, M> storage : this.storages) {
1✔
85
            T extracted = storage.extract(maxQuantity, simulate);
1✔
86
            if (!matcher.isEmpty(extracted)) {
1✔
87
                return extracted;
1✔
88
            }
NEW
89
        }
×
NEW
90
        return matcher.getEmptyInstance();
×
91
    }
92
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc