Package bm.target

Record Class StringTarget

java.lang.Object
java.lang.Record
bm.target.StringTarget
Record Components:
name - is the name of the target
content - is a method which returns the string value representing the target
dependencies - are other targets this one depends on
builder - is used to re-create the string value which can then be retrieved with content(), though usually it should be rather seldom that a builder is needed.
All Implemented Interfaces:
StateProvider, Target

public record StringTarget(String name, Supplier<String> content, List<Target> dependencies, ConsumeOrThrow<Target> builder) extends Record implements Target
A target representing a string obtained by a supplier. This is expected to be used as a dependency for other targets by, say, having this as a property value to make sure a depending target is re-created if the value of a property has changed from one run to the next. See fromProperty(java.lang.String, java.lang.String) and fromEnv(java.lang.String, java.lang.String).

Neverthless, a StringTarget may have a builder. Think of a string obtained from reading a database.

I DON'T GET IT

  1. Why is content not just a string?
  2. Why do we have it at all, and not just the builder which provides a value on demand?

As for the latter, the content provides the value of the target at the start of the build, while the builder may or may not be called to change this value. (Hardly ever needed.) For the former, the content shall fetch the string from wherever it comes during the Machine.update(bm.machine.Target) process and not already while setting up the dependency graph. This shall be the same as the content of files is not read while setting up the dependency graph.

  • Constructor Details

  • Method Details

    • fromProperty

      public static StringTarget fromProperty(String propertyName, String dflt)
      Creates an instance for the value of a property with no builder and setting the content to a method retrieving the property.
      Parameters:
      dflt - is allowed to be null
    • fromEnv

      public static StringTarget fromEnv(String varname, String dflt)
      Creates an instance for the value of an environment variable with no builder and setting the content to a method retrieving the environment variable.
      Parameters:
      dflt - is allowed to be null
    • fromMap

      public static StringTarget fromMap(Map<String,String> source, String key, String dflt)
      Creates an instance for the value from the given map with no builder and setting the content() fo a method retrieven the value from the map when requested.
    • withBuilder

      public StringTarget withBuilder(ConsumeOrThrow<Target> newBuilder)
      Returns a shallow copy of this with the new builder.
    • withDependencies

      public StringTarget withDependencies(List<Target> newDependencies)
      Returns a shallow copy of this with the new list of dependencies.
    • currentValue

      public String currentValue()
      A shortcut for content().get().
    • addState

      public void addState(Consumer<byte[]> sink)
      Description copied from interface: StateProvider
      Given a sink for byte[] this method should provide bytes representing a target's value. If the value changes, the bytes provided must change too. But the bytes may also change if the value was not actually changed, though this will result in unnecessary builts of a target.

      This should not mingle in any dependency data.

      As examples consider the full byte content of a file or only its last modified time. The latter changes even if a file is overwritten by itself, but for some cases, i.e. huge files, the time stamp may be preferable.

      Specified by:
      addState in interface StateProvider
      Parameters:
      sink - into which to fill the state representing bytes
    • getName

      public String getName()
      Description copied from interface: Target
      The name of the target, unique within the graph of targets. It is used as the key for keeping a persistent state of the target in a StateStore. Some state stores have restrictions on characters which cannot be used.

      Note: using a non unique name can only be detected during the build and only if both targets of the same name are traversed.

      Specified by:
      getName in interface Target
      Returns:
      the name
    • getDependencies

      public List<Target> getDependencies()
      Description copied from interface: Target
      Return the list of targets this one depends on when being build. This should list all targets whose change should trigger a rebuild of this target.
      Specified by:
      getDependencies in interface Target
      Returns:
      the dependencies of this target
    • recreate

      public void recreate() throws BuildProblemException
      Description copied from interface: Target
      Recreate this target.
      Specified by:
      recreate in interface Target
      Throws:
      BuildProblemException - as needed
    • toString

      public final String toString()
      Returns a string representation of this record class. The representation contains the name of the class, followed by the name and value of each of the record components.
      Specified by:
      toString in class Record
      Returns:
      a string representation of this object
    • hashCode

      public final int hashCode()
      Returns a hash code value for this object. The value is derived from the hash code of each of the record components.
      Specified by:
      hashCode in class Record
      Returns:
      a hash code value for this object
    • equals

      public final boolean equals(Object o)
      Indicates whether some other object is "equal to" this one. The objects are equal if the other object is of the same class and if all the record components are equal. All components in this record class are compared with Objects::equals(Object,Object).
      Specified by:
      equals in class Record
      Parameters:
      o - the object with which to compare
      Returns:
      true if this object is the same as the o argument; false otherwise.
    • name

      public String name()
      Returns the value of the name record component.
      Returns:
      the value of the name record component
    • content

      public Supplier<String> content()
      Returns the value of the content record component.
      Returns:
      the value of the content record component
    • dependencies

      public List<Target> dependencies()
      Returns the value of the dependencies record component.
      Returns:
      the value of the dependencies record component
    • builder

      public ConsumeOrThrow<Target> builder()
      Returns the value of the builder record component.
      Returns:
      the value of the builder record component