Script: migrate a native constructor that wraps chunks to of() static constructor

This commit is contained in:
Andreas Schildbach 2023-04-05 16:55:02 +02:00
parent 28af827c0e
commit 2444fad85b
2 changed files with 24 additions and 7 deletions

View File

@ -229,6 +229,29 @@ public class Script {
// Creation time of the associated keys, or null if unknown.
@Nullable private Instant creationTime;
/**
* Wraps given script chunks.
*
* @param chunks chunks to wrap
* @return script that wraps the chunks
*/
public static Script of(List<ScriptChunk> chunks) {
return of(chunks, TimeUtils.currentTime());
}
/**
* Wraps given script chunks.
*
* @param chunks chunks to wrap
* @param creationTime creation time of the script
* @return script that wraps the chunks
*/
public static Script of(List<ScriptChunk> chunks, Instant creationTime) {
chunks = Collections.unmodifiableList(new ArrayList<>(chunks)); // defensive copy
Objects.requireNonNull(creationTime);
return new Script(null, chunks, creationTime);
}
/**
* Construct a script that copies and wraps a given program. The array is parsed and checked for syntactic
* validity. Programs like this are e.g. used in {@link TransactionInput} and {@link TransactionOutput}.
@ -310,12 +333,6 @@ public class Script {
this.creationTime = creationTime;
}
// Used from ScriptBuilder.
Script(List<ScriptChunk> chunks, Instant creationTime) {
this.chunks = Collections.unmodifiableList(new ArrayList<>(chunks));
this.creationTime = creationTime;
}
/**
* Gets the creation time of this script, or empty if unknown.
* @return creation time of this script, or empty if unknown

View File

@ -277,7 +277,7 @@ public class ScriptBuilder {
/** Creates a new immutable Script based on the state of the builder. */
public Script build() {
return new Script(chunks, creationTime);
return Script.of(chunks, creationTime);
}
/** Creates an empty script. */