Skip to content

Commit 6642c71

Browse files
committed
Introduce RegistryValue & DefaultedTaggable, move some things around
1 parent 356e1fa commit 6642c71

File tree

4 files changed

+164
-29
lines changed

4 files changed

+164
-29
lines changed

src/main/java/org/spongepowered/api/registry/DefaultedRegistryValue.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,25 +26,52 @@
2626

2727
import org.spongepowered.api.ResourceKey;
2828

29-
import java.util.Objects;
3029
import java.util.Optional;
3130

3231
/**
33-
* A Utility marker that assists in getting a {@link ResourceKey} for values that generally can be
34-
* within a {@link DefaultedRegistryType defaulted registry}.
32+
* A {@link RegistryValue} that usually resides in a single {@link RegistryType}
33+
* and such this registry can be considered as "default".
3534
*/
36-
@SuppressWarnings("unchecked")
37-
public interface DefaultedRegistryValue {
35+
public interface DefaultedRegistryValue<T extends DefaultedRegistryValue<T>> extends RegistryValue<T> {
3836

39-
default <T> ResourceKey key(final DefaultedRegistryType<T> type) {
40-
return Objects.requireNonNull(type, "type").get().valueKey((T) this);
37+
/**
38+
* Gets the default {@link RegistryType} for
39+
* the type that implements this interface.
40+
*
41+
* @return The {@link RegistryType}
42+
*/
43+
DefaultedRegistryType<T> registryType();
44+
45+
/**
46+
* Returns the {@link ResourceKey} associated with
47+
* this object in the default {@link #registryType()}.
48+
*
49+
* @return The {@link ResourceKey} associated with
50+
* this object in the default {@link #registryType()}
51+
*/
52+
default ResourceKey registryKey() {
53+
return this.key(this.registryType());
4154
}
4255

43-
default <T> Optional<ResourceKey> findKey(final DefaultedRegistryType<T> type) {
44-
return Objects.requireNonNull(type, "type").find().flatMap(r -> r.findValueKey((T) this));
56+
/**
57+
* Returns the {@link ResourceKey} associated with
58+
* this object in the default {@link #registryType()}, if found.
59+
*
60+
* @return The {@link ResourceKey} associated with
61+
* this object in the default {@link #registryType()}, if found
62+
*/
63+
default Optional<ResourceKey> findRegistryKey() {
64+
return this.findKey(this.registryType());
4565
}
4666

47-
default <T> DefaultedRegistryReference<T> asDefaultedReference(final DefaultedRegistryType<T> type) {
48-
return RegistryKey.of(Objects.requireNonNull(type, "type"), this.key(type)).asDefaultedReference(type.defaultHolder());
67+
/**
68+
* Returns the {@link DefaultedRegistryReference} for
69+
* this object in the default {@link #registryType()}.
70+
*
71+
* @return The {@link DefaultedRegistryReference} for
72+
* this object in the default {@link #registryType()}
73+
*/
74+
default DefaultedRegistryReference<T> asDefaultedReference() {
75+
return this.asDefaultedReference(this.registryType());
4976
}
5077
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.api.registry;
26+
27+
import org.spongepowered.api.ResourceKey;
28+
29+
import java.util.Objects;
30+
import java.util.Optional;
31+
32+
/**
33+
* A Utility marker that assists in getting a {@link ResourceKey} for values that generally can be
34+
* within a {@link DefaultedRegistryType defaulted registry}.
35+
*/
36+
@SuppressWarnings("unchecked")
37+
public interface RegistryValue<T extends RegistryValue<T>> {
38+
39+
default ResourceKey key(final DefaultedRegistryType<T> type) {
40+
return Objects.requireNonNull(type, "type").get().valueKey((T) this);
41+
}
42+
43+
default Optional<ResourceKey> findKey(final DefaultedRegistryType<T> type) {
44+
return Objects.requireNonNull(type, "type").find().flatMap(r -> r.findValueKey((T) this));
45+
}
46+
47+
default DefaultedRegistryReference<T> asDefaultedReference(final DefaultedRegistryType<T> type) {
48+
return RegistryKey.of(Objects.requireNonNull(type, "type"), this.key(type)).asDefaultedReference(type.defaultHolder());
49+
}
50+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* This file is part of SpongeAPI, licensed under the MIT License (MIT).
3+
*
4+
* Copyright (c) SpongePowered <https://www.spongepowered.org>
5+
* Copyright (c) contributors
6+
*
7+
* Permission is hereby granted, free of charge, to any person obtaining a copy
8+
* of this software and associated documentation files (the "Software"), to deal
9+
* in the Software without restriction, including without limitation the rights
10+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11+
* copies of the Software, and to permit persons to whom the Software is
12+
* furnished to do so, subject to the following conditions:
13+
*
14+
* The above copyright notice and this permission notice shall be included in
15+
* all copies or substantial portions of the Software.
16+
*
17+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23+
* THE SOFTWARE.
24+
*/
25+
package org.spongepowered.api.tag;
26+
27+
import org.spongepowered.api.registry.DefaultedRegistryValue;
28+
29+
import java.util.Collection;
30+
31+
/**
32+
* A {@link DefaultedRegistryValue} that may be included in one or more {@link Tag} collections.
33+
*/
34+
public interface DefaultedTaggable<T extends DefaultedTaggable<T>> extends DefaultedRegistryValue<T>, Taggable<T> {
35+
36+
/**
37+
* Gets all {@link Tag}s that have been associated
38+
* with this object in the default {@link #registryType()}.
39+
*
40+
* @return THe {@link Collection} of {@link Tag}s
41+
*/
42+
default Collection<Tag<T>> tags() {
43+
return this.tags(this.registryType());
44+
}
45+
46+
/**
47+
* Returns whether the given {@link Tag} is associated with
48+
* this object in the default {@link #registryType()}.
49+
*
50+
* @param tag The tag
51+
* @return true if the given {@link Tag} is associated with
52+
* this object in the default {@link #registryType()}
53+
*/
54+
default boolean is(final Tag<T> tag) {
55+
return this.is(this.registryType(), tag);
56+
}
57+
}

src/main/java/org/spongepowered/api/tag/Taggable.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,36 +25,37 @@
2525
package org.spongepowered.api.tag;
2626

2727
import org.spongepowered.api.registry.DefaultedRegistryType;
28-
import org.spongepowered.api.registry.DefaultedRegistryValue;
29-
import org.spongepowered.api.registry.RegistryType;
28+
import org.spongepowered.api.registry.RegistryValue;
3029

3130
import java.util.Collection;
3231

3332
/**
34-
* A type that may be included in one or more {@link Tag} collections.
33+
* A {@link RegistryValue} that may be included in one or more {@link Tag} collections.
3534
*/
36-
public interface Taggable<T extends Taggable<T>> extends DefaultedRegistryValue {
35+
@SuppressWarnings("unchecked")
36+
public interface Taggable<T extends Taggable<T>> extends RegistryValue<T> {
3737

3838
/**
39-
* Gets the {@link RegistryType} that holds the types of {@link Tag tags}
40-
* that can be associated with this object.
39+
* Gets all {@link Tag}s that have been associated
40+
* with this object in the given registry.
4141
*
42-
* @return The {@link RegistryType}
42+
* @param type The registry type
43+
* @return THe {@link Collection} of {@link Tag}s
4344
*/
44-
DefaultedRegistryType<T> registryType();
45+
default Collection<Tag<T>> tags(final DefaultedRegistryType<T> type) {
46+
return type.get().tags().filter(tag -> this.is(type, tag)).toList();
47+
}
4548

4649
/**
47-
* Gets all {@link Tag tags} that have been associated with this object.
50+
* Returns whether the given {@link Tag} is associated
51+
* with this object in the given registry.
4852
*
49-
* @return The {@link Collection} of {@link Tag}s.
50-
*/
51-
Collection<Tag<T>> tags();
52-
53-
/**
54-
* Returns true when given tag is associated with this object
53+
* @param type The registry type
5554
* @param tag The tag
56-
* @return true when given tag is associated with this object
55+
* @return true if the given {@link Tag} is associated
56+
* with this object in the given registry
5757
*/
58-
boolean is(Tag<T> tag);
59-
58+
default boolean is(final DefaultedRegistryType<T> type, final Tag<T> tag) {
59+
return type.get().taggedValues(tag).contains((T) this);
60+
}
6061
}

0 commit comments

Comments
 (0)