File size: 2,886 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
package net.minecraft.util.context;

import com.google.common.collect.Sets;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.Set;
import javax.annotation.Nullable;
import org.jetbrains.annotations.Contract;

public class ContextMap {
    private final Map<ContextKey<?>, Object> params;

    ContextMap(Map<ContextKey<?>, Object> p_362494_) {
        this.params = p_362494_;
    }

    public boolean has(ContextKey<?> p_365248_) {
        return this.params.containsKey(p_365248_);
    }

    public <T> T getOrThrow(ContextKey<T> p_366932_) {
        T t = (T)this.params.get(p_366932_);
        if (t == null) {
            throw new NoSuchElementException(p_366932_.name().toString());
        } else {
            return t;
        }
    }

    @Nullable
    public <T> T getOptional(ContextKey<T> p_362151_) {
        return (T)this.params.get(p_362151_);
    }

    @Nullable
    @Contract("_,!null->!null; _,_->_")
    public <T> T getOrDefault(ContextKey<T> p_368195_, @Nullable T p_368521_) {
        return (T)this.params.getOrDefault(p_368195_, p_368521_);
    }

    public static class Builder {
        private final Map<ContextKey<?>, Object> params = new IdentityHashMap<>();

        public <T> ContextMap.Builder withParameter(ContextKey<T> p_369289_, T p_362485_) {
            this.params.put(p_369289_, p_362485_);
            return this;
        }

        public <T> ContextMap.Builder withOptionalParameter(ContextKey<T> p_366888_, @Nullable T p_366368_) {
            if (p_366368_ == null) {
                this.params.remove(p_366888_);
            } else {
                this.params.put(p_366888_, p_366368_);
            }

            return this;
        }

        public <T> T getParameter(ContextKey<T> p_363188_) {
            T t = (T)this.params.get(p_363188_);
            if (t == null) {
                throw new NoSuchElementException(p_363188_.name().toString());
            } else {
                return t;
            }
        }

        @Nullable
        public <T> T getOptionalParameter(ContextKey<T> p_362877_) {
            return (T)this.params.get(p_362877_);
        }

        public ContextMap create(ContextKeySet p_369803_) {
            Set<ContextKey<?>> set = Sets.difference(this.params.keySet(), p_369803_.allowed());
            if (!set.isEmpty()) {
                throw new IllegalArgumentException("Parameters not allowed in this parameter set: " + set);
            } else {
                Set<ContextKey<?>> set1 = Sets.difference(p_369803_.required(), this.params.keySet());
                if (!set1.isEmpty()) {
                    throw new IllegalArgumentException("Missing required parameters: " + set1);
                } else {
                    return new ContextMap(this.params);
                }
            }
        }
    }
}