Skip to content

Commit

Permalink
feat: Some more component changes
Browse files Browse the repository at this point in the history
* Use records to implement components
* Removed NumberComponent to favor underlying formatting systems
* Made ValueComponent standalone component for styled arguments in other components
  • Loading branch information
phinner committed May 24, 2024
1 parent 8891b93 commit bd0deed
Show file tree
Hide file tree
Showing 13 changed files with 181 additions and 276 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* Distributor, a feature-rich framework for Mindustry plugins.
*
* Copyright (C) 2024 Xpdustry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package com.xpdustry.distributor.api.component;

import com.xpdustry.distributor.api.component.style.ComponentColor;
import com.xpdustry.distributor.api.component.style.ComponentStyle;
import com.xpdustry.distributor.api.component.style.TextDecoration;
import com.xpdustry.distributor.api.permission.TriState;
import java.util.Map;
import org.checkerframework.checker.nullness.qual.Nullable;

abstract class AbstractComponentBuilder<C extends BuildableComponent<C, B>, B extends BuildableComponent.Builder<C, B>>
implements BuildableComponent.Builder<C, B> {

protected ComponentStyle.Builder style;

public AbstractComponentBuilder(final C component) {
this.style = component.getStyle().toBuilder();
}

public AbstractComponentBuilder() {
this.style = ComponentStyle.builder();
}

@SuppressWarnings("unchecked")
@Override
public B setTextColor(final @Nullable ComponentColor textColor) {
style.setTextColor(textColor);
return (B) this;
}

@SuppressWarnings("unchecked")
@Override
public B setBackColor(final @Nullable ComponentColor backColor) {
style.setBackColor(backColor);
return (B) this;
}

@SuppressWarnings("unchecked")
@Override
public B setDecorations(final Map<TextDecoration, ? extends Boolean> decorations) {
style.setDecorations(decorations);
return (B) this;
}

@SuppressWarnings("unchecked")
@Override
public B setDecoration(final TextDecoration decoration, final TriState state) {
style.setDecoration(decoration, state);
return (B) this;
}

@SuppressWarnings("unchecked")
@Override
public B setStyle(final ComponentStyle style) {
this.style = style.toBuilder();
return (B) this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,11 @@
import com.xpdustry.distributor.api.component.style.ComponentStyle;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

final class ListComponentImpl extends AbstractComponent<ListComponent, ListComponent.Builder> implements ListComponent {

private final List<Component> components;
record ListComponentImpl(ComponentStyle style, List<Component> components) implements ListComponent {

ListComponentImpl(final ComponentStyle style, final List<Component> components) {
super(style);
this.style = style;
this.components = List.copyOf(components);
}

Expand All @@ -38,28 +35,16 @@ public List<Component> getComponents() {
}

@Override
public ListComponent.Builder toBuilder() {
return new Builder(this);
}

@Override
public String toString() {
return "ListComponent{style=" + getStyle() + ", components=" + components + "}";
public ComponentStyle getStyle() {
return this.style;
}

@Override
public int hashCode() {
return Objects.hash(getStyle(), components);
}

@Override
public boolean equals(final Object o) {
return (o instanceof ListComponentImpl other)
&& getStyle().equals(other.getStyle())
&& components.equals(other.components);
public Builder toBuilder() {
return new Builder(this);
}

static final class Builder extends AbstractComponent.Builder<ListComponent, ListComponent.Builder>
static final class Builder extends AbstractComponentBuilder<ListComponent, ListComponent.Builder>
implements ListComponent.Builder {

private final List<Component> components;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,19 @@
import java.time.format.FormatStyle;
import java.time.temporal.Temporal;

public interface TemporalComponent
extends BuildableComponent<TemporalComponent, TemporalComponent.Builder>, ValueComponent<Temporal> {
public interface TemporalComponent extends BuildableComponent<TemporalComponent, TemporalComponent.Builder> {

static TemporalComponent.Builder temporal() {
return new TemporalComponentImpl.Builder();
}

static TemporalComponent temporal(final Temporal temporal, final TemporalFormat fallbackFormat) {
return new TemporalComponentImpl(ComponentStyle.empty(), temporal, fallbackFormat);
static TemporalComponent temporal(final Temporal temporal, final TemporalFormat format) {
return new TemporalComponentImpl(ComponentStyle.empty(), temporal, format);
}

static TemporalComponent temporal(
final Temporal temporal, final TemporalFormat fallbackFormat, final ComponentColor textColor) {
return new TemporalComponentImpl(ComponentStyle.style(textColor), temporal, fallbackFormat);
final Temporal temporal, final TemporalFormat format, final ComponentColor textColor) {
return new TemporalComponentImpl(ComponentStyle.style(textColor), temporal, format);
}

static TemporalComponent temporal(final Temporal temporal) {
Expand All @@ -51,17 +50,12 @@ static TemporalComponent temporal(final Temporal temporal, final ComponentColor

Temporal getTemporal();

TemporalFormat getFallbackFormat();

@Override
default Temporal getValue() {
return getTemporal();
}
TemporalFormat getFormat();

interface Builder extends BuildableComponent.Builder<TemporalComponent, TemporalComponent.Builder> {

Builder setTemporal(final Temporal temporal);

Builder setFallbackFormat(final TemporalFormat fallbackFormat);
Builder setFormat(final TemporalFormat format);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,38 +23,34 @@
import java.time.format.FormatStyle;
import java.time.temporal.Temporal;

final class TemporalComponentImpl extends AbstractComponent<TemporalComponent, TemporalComponent.Builder>
record TemporalComponentImpl(ComponentStyle style, Temporal temporal, TemporalFormat format)
implements TemporalComponent {

private final Temporal temporal;
private final TemporalFormat fallbackStyle;

TemporalComponentImpl(final ComponentStyle style, final Temporal temporal, final TemporalFormat fallbackStyle) {
super(style);
this.temporal = temporal;
this.fallbackStyle = fallbackStyle;
}

@Override
public Temporal getTemporal() {
return this.temporal;
}

@Override
public TemporalFormat getFallbackFormat() {
return fallbackStyle;
public TemporalFormat getFormat() {
return format;
}

@Override
public ComponentStyle getStyle() {
return style;
}

@Override
public TemporalComponent.Builder toBuilder() {
public Builder toBuilder() {
return new Builder(this);
}

static final class Builder extends AbstractComponent.Builder<TemporalComponent, TemporalComponent.Builder>
static final class Builder extends AbstractComponentBuilder<TemporalComponent, TemporalComponent.Builder>
implements TemporalComponent.Builder {

private Temporal temporal;
private TemporalFormat fallbackFormat = TemporalFormat.ofDateTime(FormatStyle.SHORT);
private TemporalFormat format = TemporalFormat.ofDateTime(FormatStyle.SHORT);

public Builder() {
this.temporal = Instant.now();
Expand All @@ -72,14 +68,14 @@ public Builder setTemporal(final Temporal temporal) {
}

@Override
public Builder setFallbackFormat(final TemporalFormat fallbackFormat) {
this.fallbackFormat = fallbackFormat;
public Builder setFormat(final TemporalFormat format) {
this.format = format;
return this;
}

@Override
public TemporalComponent build() {
return new TemporalComponentImpl(style.build(), temporal, fallbackFormat);
return new TemporalComponentImpl(style.build(), temporal, format);
}
}
}
Loading

0 comments on commit bd0deed

Please sign in to comment.