#Generic Help!

1 messages · Page 1 of 1 (latest)

shadow oxide
#

I will try to explain my problem in the least amount of code possible. I am trying to avoid casting in this so beware!

public interface AttributeData {
    /*Marker for Data Class For the Data*/
}```

```java
public sealed interface AttributeHandler<T extends AttributeData>
Class<T> dataClass();
public class AttributeRegistry extends ImmutableRegistry<Key, AttributeHandler<? extends AttributeData>>
private final ObjectArrayList<AttributeData> attributes;
    public <T extends AttributeData> Optional<T> get(Class<T> clazz) {
        for (AttributeData attr : attributes) {
            if (attr.getClass() == clazz) {
                //noinspection unchecked
                return Optional.of((T) attr);
            }
        }
        return Optional.empty();
    }```


```java
public non-sealed interface ModifierHandler<T extends AttributeData> extends AttributeHandler<T> {

    StatProfile modifierStats(SkyblockItem item, T data);```

```java
    public void process(Container container) {
        // Process Stats First
        for (AttributeHandler<? extends AttributeData> value : registry.values()) {
            if (value instanceof ModifierHandler<? extends AttributeData> modifierHandler) {
                container.get(value.dataClass()).ifPresent(data -> {
                    modifierHandler.modifierStats(null, data);
                });
           }
        }
    }```

Error attached as image
red voidBOT
#

<@&987246399047479336> please have a look, thanks.

shadow oxide
#

I think that is all the relevant code for generics

tulip forum
#

I think I see the words Attribute, Registry, Data, Handler, Immutable, Modifier. What a horrible soup of non-descriptive-ness.

shadow oxide
#

I could always take recommendations for names :)

tulip forum
#

No one wants to look at any code with zero context. What is your question.

shadow oxide
#

I attached the error :l

tulip forum
#

If you can't explain what your code does then you don't understand what you're doing. How about what you think it does.

#

This all looks like 'grand architect' syndrome.

shadow oxide
#

what do u want me to explain

tulip forum
#

What is all this junk. Why can't you explain your code? I am launching a missile at your chair. Incoming in 15min.

shadow oxide
#

AttributeData -> Marker class for data held by items usually a record.

AttributeHandler -> Handles all the operations regarding UI, calculations, serialization/de serialization for the data

AttributeRegistry -> Holds all the AttributeHandlers

ObjectArraylist & get method -> A container that holds all the AttributeData a glorified list with helper methods to easily manage the AttributeDaya

ModifierHandler -> An extension for Handler it focuses on calculations part. Each handler has difference purpose.

Process -> We process all different type of handlers to have the final information we need which is processed by the handlers.

tulip forum
#

How does the error message fit in to this

#

wait what's this AttributeHandler<? why don't you know what this is?

shadow oxide
#

AttributeHandler<? extends AttributeData>> this?

tulip forum
#

Yes, that is your capture error.

shadow oxide
#

I didn't know what else to put there tbh

#

public sealed interface AttributeHandler<T extends AttributeData> here we made sure that it uses AttributeData only so I just put a wild card at the Registry

tulip forum
#

Why is it called AttributeRegistry if its actually an AttributeHandler it's holding.

shadow oxide
#

AttributeHandlerRegistry was too big name

tulip forum
#

Names have meaning I guess. For example AttributeData is a marker class yet the word Attribute implies a name of some aspect and the word Data implies the aspect value. Yet it has neither.

#

There is neither a method to get the name or the value

shadow oxide
tulip forum
#

then you are casting.

#

Have you seen the Collectors class?

#

like 5 type parameters.

#

to build a bespoke collector

#

You can give the type parameters real names. You don't have to use T for everything.

shadow oxide
#

I am aware

tulip forum
#

AttributeData just sounds like Map.Entry

shadow oxide
#

Map.Entry is slightly different.

shadow oxide
#

Not the best fix perhaps but I was tired of various generic combos

red voidBOT
#

Closed the thread.