Skip to content

Commit

Permalink
Song, Layer, Note and CustomInstrument are immutable and created usin…
Browse files Browse the repository at this point in the history
…g builders.
  • Loading branch information
koca2000 committed Jul 9, 2023
1 parent 661a073 commit 1180728
Show file tree
Hide file tree
Showing 11 changed files with 764 additions and 1,013 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>cz.koca2000</groupId>
<artifactId>NBS4j</artifactId>
<version>1.3.0</version>
<version>2.0.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>8</maven.compiler.source>
Expand Down
177 changes: 69 additions & 108 deletions src/main/java/cz/koca2000/nbs4j/CustomInstrument.java
Original file line number Diff line number Diff line change
@@ -1,102 +1,19 @@
package cz.koca2000.nbs4j;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public class CustomInstrument {

private String name = "";
private String fileName = "";
private int key = 0;
private boolean shouldPressKey = false;
private final String name;
private final String fileName;
private final int key;
private final boolean shouldPressKey;

private boolean isFrozen = false;
private Song song;

public CustomInstrument(){}

/**
* Creates a copy of the custom instrument. Copy is not frozen and does not belong to any song.
* @param customInstrument custom instrument to be copied
*/
public CustomInstrument(@NotNull CustomInstrument customInstrument){
name = customInstrument.name;
fileName = customInstrument.fileName;
key = customInstrument.key;
shouldPressKey = customInstrument.shouldPressKey;

isFrozen = false;
song = null;
}

@NotNull
CustomInstrument setSong(@NotNull Song song){
if (this.song != null)
throw new IllegalStateException("Custom instrument was already added to a song.");

this.song = song;
return this;
}

/**
* Sets the name of this custom instrument
* @param name name of the custom instrument
* @return this instance of {@link CustomInstrument}
* @throws IllegalArgumentException if the argument is null.
* @throws IllegalStateException if the custom instrument is frozen and can not be modified
*/
@NotNull
public CustomInstrument setName(@NotNull String name){
throwIfFrozen();

this.name = name;
return this;
}

/**
* Sets the name of the file used in OpenNoteBlockStudio
* @param fileName name of the file
* @return this instance of {@link CustomInstrument}
* @throws IllegalArgumentException if the argument is null.
* @throws IllegalStateException if the custom instrument is frozen and can not be modified
*/
@NotNull
public CustomInstrument setFileName(@NotNull String fileName){
throwIfFrozen();

this.fileName = fileName;
return this;
}

/**
* Key of this custom instrument.
* @param key Value 0 is A0 and 87 is C8.
* @return this instance of {@link CustomInstrument}
* @throws IllegalArgumentException if the argument is not in range [0; 87] inclusive.
* @throws IllegalStateException if the custom instrument is frozen and can not be modified
*/
@NotNull
public CustomInstrument setKey(int key){
throwIfFrozen();

if (key < 0 || key > 87)
throw new IllegalArgumentException("Key must be in range [0; 87].");
this.key = key;
return this;
}

/**
* Sets whether OpenNoteBlockStudio should press key on piano when playing this instrument's note.
* @param shouldPressKey true if the key should be pressed; otherwise, false
* @return this instance of {@link CustomInstrument}
* @throws IllegalStateException if the custom instrument is frozen and can not be modified
*/
@NotNull
public CustomInstrument setShouldPressKey(boolean shouldPressKey) {
throwIfFrozen();

this.shouldPressKey = shouldPressKey;
return this;
private CustomInstrument(@NotNull Builder builder){
name = builder.name;
fileName = builder.fileName;
key = builder.key;
shouldPressKey = builder.shouldPressKey;
}

/**
Expand Down Expand Up @@ -125,15 +42,6 @@ public int getKey() {
return key;
}

/**
* Returns the song this custom instrument belongs to.
* @return {@link Song} if the custom instrument was added to a song; otherwise, null
*/
@Nullable
public Song getSong() {
return song;
}

/**
* Returns whether OpenNoteBlockStudio should press key on piano when playing this instrument's note.
* @return true if the key should be pressed; otherwise, false
Expand All @@ -142,12 +50,65 @@ public boolean shouldPressKey() {
return shouldPressKey;
}

void freeze(){
isFrozen = true;
}

private void throwIfFrozen(){
if (isFrozen)
throw new IllegalStateException("Custom instrument is frozen and can not be modified.");
public static final class Builder {
private String name = "";
private String fileName = "";
private int key = 0;
private boolean shouldPressKey = false;

/**
* Sets the name of this custom instrument
* @param name name of the custom instrument
* @return this instance of {@link Builder}
*/
@NotNull
public Builder setName(@NotNull String name){
this.name = name;
return this;
}

/**
* Sets the name of the file used in OpenNoteBlockStudio
* @param fileName name of the file
* @return this instance of {@link Builder}
*/
@NotNull
public Builder setFileName(@NotNull String fileName){
this.fileName = fileName;
return this;
}

/**
* Key of this custom instrument.
* @param key Value 0 is A0 and 87 is C8.
* @return this instance of {@link Builder}
* @throws IllegalArgumentException if the argument is not in range [0; 87] inclusive.
*/
@NotNull
public Builder setKey(int key){
if (key < 0 || key > 87)
throw new IllegalArgumentException("Key must be in range [0; 87].");
this.key = key;
return this;
}

/**
* Sets whether OpenNoteBlockStudio should press key on piano when playing this instrument's note.
* @param shouldPressKey true if the key should be pressed; otherwise, false
* @return this instance of {@link Builder}
*/
@NotNull
public Builder setShouldPressKey(boolean shouldPressKey) {
this.shouldPressKey = shouldPressKey;
return this;
}

/**
* Creates new instance of {@link CustomInstrument} based on data from this builder.
* @return {@link CustomInstrument}
*/
public CustomInstrument build() {
return new CustomInstrument(this);
}
}
}
Loading

0 comments on commit 1180728

Please sign in to comment.