Skip to content

Commit

Permalink
change pattern for creating builder from existing instance
Browse files Browse the repository at this point in the history
  • Loading branch information
zml2008 committed Jun 9, 2022
1 parent d7a9dc3 commit 8372ba9
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 44 deletions.
19 changes: 11 additions & 8 deletions api/src/main/java/net/kyori/adventure/sound/Sound.java
Expand Up @@ -69,6 +69,17 @@ public interface Sound extends Examinable {
return new SoundImpl.BuilderImpl();
}

/**
* Create a new builder for {@link Sound} instances.
*
* @param existing an existing sound to populate the builder with
* @return a new builder
* @since 4.12.0
*/
static @NotNull Builder sound(final @NotNull Sound existing) {
return new SoundImpl.BuilderImpl(existing);
}

/**
* Create a new {@link Sound} instance configured by the provided function.
*
Expand Down Expand Up @@ -215,14 +226,6 @@ public interface Sound extends Examinable {
*/
@NotNull SoundStop asStop();

/**
* Create a new builder populated with values from this sound instance.
*
* @return a new builder
* @since 4.12.0
*/
@NotNull Builder toBuilder();

/**
* The sound source.
*
Expand Down
85 changes: 49 additions & 36 deletions api/src/main/java/net/kyori/adventure/sound/SoundImpl.java
Expand Up @@ -28,7 +28,6 @@
import java.util.stream.Stream;
import net.kyori.adventure.internal.Internals;
import net.kyori.adventure.key.Key;
import net.kyori.adventure.sound.Sound.Source.Provider;
import net.kyori.adventure.util.ShadyPines;
import net.kyori.examination.ExaminableProperty;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -132,6 +131,24 @@ static final class BuilderImpl implements Builder {
private float pitch = DEFAULT_PITCH;
private OptionalLong seed = OptionalLong.empty();

BuilderImpl() {
}

BuilderImpl(final @NotNull Sound existing) {
if (existing instanceof Eager) {
this.type(((Eager) existing).name);
} else if (existing instanceof Lazy) {
this.type(((Lazy) existing).supplier);
} else {
throw new IllegalArgumentException("Unknown sound type " + existing + ", must be Eager or Lazy");
}

this.source(existing.source())
.volume(existing.volume())
.pitch(existing.pitch())
.seed(existing.seed());
}

@Override
public @NotNull Builder type(final @NotNull Key type) {
this.eagerType = requireNonNull(type, "type");
Expand Down Expand Up @@ -160,7 +177,7 @@ static final class BuilderImpl implements Builder {
}

@Override
public @NotNull Builder source(final @NotNull Provider source) {
public @NotNull Builder source(final Source.@NotNull Provider source) {
return this.source(source.soundSource());
}

Expand Down Expand Up @@ -191,44 +208,40 @@ static final class BuilderImpl implements Builder {
@Override
public @NotNull Sound build() {
if (this.eagerType != null) {
final Key name = this.eagerType;
return new SoundImpl(this.source, this.volume, this.pitch, this.seed) {
@Override
public @NotNull Key name() {
return name;
}

@Override
public @NotNull Builder toBuilder() {
return Sound.sound()
.type(this.name())
.source(this.source())
.volume(this.volume())
.pitch(this.pitch())
.seed(this.seed());
}
};
return new Eager(this.eagerType, this.source, this.volume, this.pitch, this.seed);
} else if (this.lazyType != null) {
final Supplier<? extends Type> nameSupplier = this.lazyType;
return new SoundImpl(this.source, this.volume, this.pitch, this.seed) {
@Override
public @NotNull Key name() {
return nameSupplier.get().key();
}

@Override
public @NotNull Builder toBuilder() {
return Sound.sound()
.type(nameSupplier)
.source(this.source())
.volume(this.volume())
.pitch(this.pitch())
.seed(this.seed());
}
};
return new Lazy(this.lazyType, this.source, this.volume, this.pitch, this.seed);
} else {
throw new IllegalStateException("A sound type must be provided to build a sound");
}
}
}

static final class Eager extends SoundImpl {
final Key name;

Eager(final @NotNull Key name, final @NotNull Source source, final float volume, final float pitch, final OptionalLong seed) {
super(source, volume, pitch, seed);
this.name = name;
}

@Override
public @NotNull Key name() {
return this.name;
}
}

static final class Lazy extends SoundImpl {
final Supplier<? extends Type> supplier;

Lazy(final @NotNull Supplier<? extends Type> supplier, final @NotNull Source source, final float volume, final float pitch, final OptionalLong seed) {
super(source, volume, pitch, seed);
this.supplier = supplier;
}

@Override
public @NotNull Key name() {
return this.supplier.get().key();
}
}
}

0 comments on commit 8372ba9

Please sign in to comment.