Class SimpleMessage<D>

java.lang.Object
eu.bandm.tools.message.Message
eu.bandm.tools.message.SimpleMessage<D>
Type Parameters:
D - the document identifier used by the locations
All Implemented Interfaces:
Serializable
Direct Known Subclasses:
SimpleMessage.Ping

public class SimpleMessage<D> extends Message
A minimal selection of message data which is useful in practice. Used ubiquituously in metatools.
Very small, but in most cases sufficient information content: The abstract Message class is enhanced by a Location, which points to a document(/some documents), a textual information text, and a list of arguments (=array of objects) which probably will be "sprintf"-ed into the message.

Additionally this class provides a zoo of static factory methods. But the "create and send" methods of SimpleMessage.Sender should be preferred when possible.
See Also:
  • Field Details

    • location

      @Opt protected final @Opt Location<D> location
      Optional document position the message is related to.
    • text

      @Opt protected final @Opt String text
      Contains the textual information of the message (maybe null). ("text" and "cause" can both be ==null in case of a simple "Ping"-style message.)
    • args

      @Opt protected final @Opt Object[] args
    • emptyArgs

      public static final Object[] emptyArgs
  • Constructor Details

    • SimpleMessage

      public SimpleMessage(Message.Kind k, @Opt @Opt Throwable e, @Opt @Opt Locatable<D> l, @Opt @Opt String t, Object... a)
      Main constructor which sets all field values.
      ATTENTION: Even both e and t maybe =null for a "Ping"-style message.
      Parameters:
      k - (never null) message Kind
      e - (maybe null) causing Throwable
      l - (maybe null) text location to which the message is related
      t - (maybe null) message text
      a - (maybe null) list of additional pieces of information, meant to be printf-ed into the message.
      ATTENTION: The value "null" is explicitly replaced by an empty array. For an arg-list with one single entry of value "null" use "(Object)null".
    • SimpleMessage

      public SimpleMessage(SimpleMessage<D> msg)
      return a clone.
  • Method Details

    • getLocation

      @Opt public final @Opt Location<D> getLocation()
      Returns:
      (maybe null) the text location related to the message.
    • getText

      @Opt public final @Opt String getText()
      Returns:
      (maybe null) the text contents related to the message.
    • getArgs

      public final Object[] getArgs()
      Returns:
      the argument array contained in this message. Is never null, but possibly empty.
    • messageToString

      public static <D> Function<SimpleMessage<D>,String> messageToString(Function<? super Location<D>,? extends String> formatLoc)
      Create a reified function for converting a message to a string, using a given transformation function for the Location part. Result is meant to be plugged in MessagePrinter.setMessageToString(Function).
    • messageToString

      public static <D> Function<SimpleMessage<D>,String> messageToString(Function<? super Location<D>,? extends String> formatLoc, Function<? super Message.Kind,String> formatKind)
      Construct a reified function for converting a message to a string, using the given transformation functions for the Location part and for the Message.Kind. Result is meant to be plugged in MessagePrinter.setMessageToString(Function).
    • toString

      public String toString(Function<? super Location<D>,? extends String> formatLoc, Function<? super Message.Kind,String> formatKind)
      Convert a message to a string, using the function arguments to print the Location and the Message.Kind parts. This is only a rough interpretation for debugging and tracing; an actual user-level MessagePrinter provides more elaborate methods. Esp. no "printf"-style of the argument list is executed.
    • toString

      public String toString(Function<? super Location<D>,? extends String> formatLoc)
      Convert a message to a string, using the function argument to print the Location part and the enumeration names for the Message.Kind.
    • toString

      public String toString()
      Convert a message to a string, using the java default "toString(Object)" for the Location part. This is a primitive visualisation, required for tracing and debugging. An actual user-level MessagePrinter or sim., will provide much more elaborate rendering.
      Overrides:
      toString in class Message
    • cast

      public static <E> SimpleMessage<E> cast(SimpleMessage<? extends E> message)
      Convenience method for upcasting any instance with a more specialized (=sub-class) document identifier to an instance with the more general document identifier. When used as method argument, return value or in assignments, the target class needs not be given explicitly, as in
      SimpleMessage<Object> var = cast(new SimpleMessage<String>())
      Otherwise it can be called explicitly:
      SimpleMessage.<Object>cast(new SimpleMessage<String>())
      Returns:
      loc
    • narrow

      public static <E> MessageReceiver<SimpleMessage<E>> narrow(MessageReceiver<? super SimpleMessage<? super E>> receiver)
      Convenience method for downcasting a message receiver accepting more general messages or document identifiers to one which accepts more specialized (=sub-class) messages. When used as method argument, return value or in assignments, the target class needs not be given explicitly, as in
      MessageReceiver<SimpleMessage<String>> rec = narrow(new MessageReceiver<Message>())
      Otherwise it can be called explicitly:
      SimpleMessage.<SimpleMessage<String>>narrow(new MessageReceiver<Message>())
      Returns:
      loc
    • narrowToBottom

      public static <D> MessageReceiver<SimpleMessage<?>> narrowToBottom(MessageReceiver<? super SimpleMessage<D>> receiver)
      Convenience method for casting a message receiver which accepts SimpleMessages with a particular document identifiers to one with an unspecified document identifier.
    • mapKind

      public SimpleMessage<D> mapKind(UnaryOperator<Message.Kind> f)
      Apply a function which operates on the Message.Kind part to construct a new message.
    • liftMapKind

      public static <X> Function<SimpleMessage<X>,SimpleMessage<X>> liftMapKind(UnaryOperator<Message.Kind> f)
      Lift a function which operates on the Message.Kind part to one which operates on the whole Message.
    • mapLocation

      public <E> SimpleMessage<E> mapLocation(Function<Location<D>,Location<E>> f)
      Apply a function which operates on the Location part to construct a new message. Consider when defining the function, that location maybe null.
    • liftMapLocation

      public static <D, E> Function<SimpleMessage<D>,SimpleMessage<E>> liftMapLocation(Function<Location<D>,Location<E>> f)
      Constructs a function which maps SimpleMessages (never null to never null), when a function which maps their Location (maybe null to maybe null) is given. Partiality/totality is also lifted, unaltered.
      (This function is used by e.g. xslt/base/TPathCache, d2d2/base/Main+others.)
    • mapLocation

      public static <E, F> BiFunction<Function<Location<E>,Location<F>>,SimpleMessage<E>,SimpleMessage<F>> mapLocation()
      A BiFunction which delivers a clone of a SimpleMessage and applies a mapping function on the Location part. Is used by LocationMap.
    • mapText

      public SimpleMessage<D> mapText(UnaryOperator<String> f)
      Apply a function which operates on the text part to construct a new message.
    • liftMapText

      public static <X> Function<SimpleMessage<X>,SimpleMessage<X>> liftMapText(UnaryOperator<String> f)
      Lift a function which operates on the text part to one which operates on the whole Message.
    • log

      public static <D> SimpleMessage<D> log(String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • log

      public static <D> SimpleMessage<D> log(Locatable<D> loc, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • log

      public static <D> SimpleMessage<D> log(Throwable ex, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • log

      public static <D> SimpleMessage<D> log(Throwable ex, Locatable<D> loc, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes
    • logStart

      public static <D> SimpleMessage<D> logStart(String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • logStart

      public static <D> SimpleMessage<D> logStart(Locatable<D> loc, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • logStart

      public static <D> SimpleMessage<D> logStart(Throwable ex, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • logStart

      public static <D> SimpleMessage<D> logStart(Throwable ex, Locatable<D> loc, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes
    • logEnd

      public static <D> SimpleMessage<D> logEnd(String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • logEnd

      public static <D> SimpleMessage<D> logEnd(Locatable<D> loc, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • logEnd

      public static <D> SimpleMessage<D> logEnd(Throwable ex, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • logEnd

      public static <D> SimpleMessage<D> logEnd(Throwable ex, Locatable<D> loc, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes
    • error

      public static <D> SimpleMessage<D> error(String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • error

      public static <D> SimpleMessage<D> error(Locatable<D> loc, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • error

      public static <D> SimpleMessage<D> error(Throwable ex, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • error

      public static <D> SimpleMessage<D> error(Throwable ex, Locatable<D> loc, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes
    • warning

      public static <D> SimpleMessage<D> warning(String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • warning

      public static <D> SimpleMessage<D> warning(Locatable<D> loc, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • warning

      public static <D> SimpleMessage<D> warning(Throwable ex, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • warning

      public static <D> SimpleMessage<D> warning(Throwable ex, Locatable<D> loc, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes
    • failure

      public static <D> SimpleMessage<D> failure(String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • failure

      public static <D> SimpleMessage<D> failure(Locatable<D> loc, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • failure

      public static <D> SimpleMessage<D> failure(Throwable ex, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • failure

      public static <D> SimpleMessage<D> failure(Throwable ex, Locatable<D> loc, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes
    • hint

      public static <D> SimpleMessage<D> hint(String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • hint

      public static <D> SimpleMessage<D> hint(Locatable<D> loc, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • hint

      public static <D> SimpleMessage<D> hint(Throwable ex, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes, and set all others to default.
    • hint

      public static <D> SimpleMessage<D> hint(Throwable ex, Locatable<D> loc, String message, Object... args)
      Convenience factory Method: Construct a message with the given kind and attributes
    • ping

      public static <X> SimpleMessage.Ping<X> ping()
      Convenience factory Method: Create an instance of SimpleMessage.Ping, which is of kind Message.Kind.log.
    • monitor

      public static <D> Thread monitor(Thread thread, MessageReceiver<? super SimpleMessage<D>> receiver)
      Installs a handler on the given thread that reports uncaught exceptions as failure messages. Is equal to monitor(thread, receiver, false).
    • monitor

      public static <D> Thread monitor(Thread thread, MessageReceiver<? super SimpleMessage<D>> receiver, @Opt PrintStream ps)
      Installs a handler on the given thread that reports uncaught exceptions as failure messages.
      Type Parameters:
      D - the document identifier type
      Parameters:
      thread - the thread to monitor
      receiver - the receiver for the failure message
      ps - if != null, additionally the stack trace will be printed there.
      Returns:
      thread
      See Also: