# UnimplementedFeature.java

    /*
    ** Module   : UnimplementedFeature.java
    ** Abstract : helper class to cleanly log runtime usage of code that is unimplemented or partial
    **
    ** Copyright (c) 2014-2023, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- --------------------------------Description----------------------------------
    ** 001 GES 20141029 Created initial version.
    ** 002 GES 20141118 Added unsupported() method. Synchronized access to the token set.
    ** 003 HC  20171024 Added 'experimental' set of methods.
    ** 004 ECF 20171120 Only log stack trace if FINE (or higher) logging level is enabled. Otherwise,
    **                  log only the message text.
    ** 005 GBB 20230512 Logging methods replaced by CentralLogger/ConversionStatus.
    */
    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 of the
    ** License, or (at your option) any later version.
    **
    ** This program is distributed in the hope that it will be useful,
    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.util;

    import com.goldencode.p2j.util.logging.*;

    import java.util.logging.*;
    import java.util.*;

    /**
     * Helper class to cleanly log runtime usage of code that is unimplemented or partial.
     * Each log entry will contain a description as well as a stack trace identifying the call
     * path that tried to use the unimplemented feature.
     * <p>
     * <b>Usage Guidelines</b>
     * <p>
     * <ul>
     *   <li> {@link #missing} should be used for methods/features which have no substantial
     *        implementation provided.
     *   <li> {@link #partial} should be used for methods/features which have some implementation
     *        but it is not complete.  If possible, place the call to <code>partial</code> inside
     *        code that only executes when the missing portions of the implementation would be
     *        executed.
     *   <li> {@link #todo} should be used for anything that needs further review, investigation
     *        or modification.  If possible, place the call to <code>todo</code> inside code
     *        that only executes when the associated condition presents itself.
     *   <li> {@link #unsupported} should be used for anything feature that exists as a placeholder
     *        but which is not ever planned to be supported.
     *   <li> There are forms of each method that take and return a token.  Use these forms if
     *        the logging should only occur the first time the code is executed. This is useful
     *        for features that get called with frequency such that the log file would get filled
     *        with "noise" from excess logging.
     *   <li> When using tokens, they should generally be unique to the specific calling location
     *        otherwise you will have log entries suppressed from unrelated calls to this class.
     *   <li> When the feature is actually implemented, the logging call to this class would
     *        naturally be remvoed.  <b>Don't forget to delete any associated token data members
     *        at that time!</b>
     * </ul>
     * <p>
     * Example of using tokens:
     * <p>
     * <pre>
     * private static Object token = null;
     *
     * public void someMethod()
     * {
     *    if (someCondition)
     *    {
     *       // the first time this is called, the token is null and the log entry will be made
     *       // but subsequent calls will pass the token returned from the first call and the
     *       // logging will be suppressed
     *       token = UnimplementedFeature.todo("This is a description of the TODO.", token);
     *    }
     * }
     * </pre>
     */
    public class UnimplementedFeature
    {
       /** Logger */
       private static final CentralLogger logger = CentralLogger.get(UnimplementedFeature.class);

       /** Set of all tokens provided to callers. */
       private static Set<Object> tokens = new HashSet<>();

       /**
        * Log the given message along with a given stack trace and a prefix that indicates that
        * a feature was used at runtime for which the runtime support is completely missing. The
        * logging is unconditionally done (there is no logging level checked) with the level set
        * to <code>SEVERE</code>.
        * <p>
        * Since a call stack will be created and logged, the location of the missing feature will
        * already be readily apparent.
        *
        * @param    msg
        *           A description of the missing feature.
        */
       public static void missing(String msg)
       {
          log(Level.SEVERE, "MISSING", msg);
       }

       /**
        * Conditionally log the given message if the caller's provided token is <code>null</code>
        * or if it was not a valid token.  Subsequent calls to this method will silently return
        * without logging if the given token already exists in the set of valid tokens. Any log entry
        * will have the given text along with the a stack trace and a prefix that indicates that
        * a feature was used at runtime for which the runtime support is completely missing. The
        * logging is unconditionally done (there is no logging level checked) with the level set
        * to <code>SEVERE</code>.
        * <p>
        * Since a call stack will be created and logged, the location of the missing feature will
        * already be readily apparent.
        *
        * @param    msg
        *           A description of the missing feature.
        * @param    token
        *           A previously provided token value that uniquely distinguishes the caller
        *           or <code>null</code> if this is the first call.
        *
        * @return   The given token if it is a valid previously provided token.  Otherwise a newly
        *           created token will be associated with the caller and returned.
        */
       public static Object missing(String msg, Object token)
       {
          synchronized (UnimplementedFeature.class)
          {
             if (token == null || !tokens.contains(token))
             {
                token = new Object();
                tokens.add(token);
                missing(msg);
             }
          }

          return token;
       }

       /**
        * Log the given message along with the a stack trace and a prefix that indicates that
        * a feature was used at runtime for which the runtime support is only partially implemented.
        * The logging is unconditionally done (there is no logging level checked) with the level
        * set to <code>SEVERE</code>.
        * <p>
        * Since a call stack will be created and logged, the location of the partial feature will
        * already be readily apparent.
        *
        * @param    msg
        *           A description of the missing feature.
        */
       public static void partial(String msg)
       {
          log(Level.SEVERE, "PARTIAL", msg);
       }

       /**
        * Conditionally log the given message if the caller's provided token is <code>null</code>
        * or if it was not a valid token.  Subsequent calls to this method will silently return
        * without logging if the given token already exists in the set of valid tokens. Any log entry
        * will have the given text along with the a stack trace and a prefix that indicates that
        * a feature was used at runtime for which the runtime support is only partially implemented.
        * The logging is unconditionally done (there is no logging level checked) with the level
        * set to <code>SEVERE</code>.
        * <p>
        * Since a call stack will be created and logged, the location of the partial feature will
        * already be readily apparent.
        *
        * @param    msg
        *           A description of the missing feature.
        * @param    token
        *           A previously provided token value that uniquely distinguishes the caller
        *           or <code>null</code> if this is the first call.
        *
        * @return   The given token if it is a valid previously provided token.  Otherwise a newly
        *           created token will be associated with the caller and returned.
        */
       public static Object partial(String msg, Object token)
       {
          synchronized (UnimplementedFeature.class)
          {
             if (token == null || !tokens.contains(token))
             {
                token = new Object();
                tokens.add(token);
                partial(msg);
             }
          }

          return token;
       }

       /**
        * Log the given message along with the a stack trace and a prefix that indicates that
        * a feature was used at runtime for which the runtime support is at an experimental level.
        * The logging is unconditionally done (there is no logging level checked) with the level
        * set to <code>SEVERE</code>.
        * <p>
        * Since a call stack will be created and logged, the location of the experimental feature will
        * already be readily apparent.
        *
        * @param    msg
        *           A description of the experimental feature.
        */
       public static void experimental(String msg)
       {
          log(Level.SEVERE, "EXPERIMENTAL", msg);
       }

       /**
        * Conditionally log the given message if the caller's provided token is <code>null</code>
        * or if it was not a valid token.  Subsequent calls to this method will silently return
        * without logging if the given token already exists in the set of valid tokens. Any log entry
        * will have the given text along with the a stack trace and a prefix that indicates that
        * a feature was used at runtime for which the runtime support is at an experimental level.
        * The logging is unconditionally done (there is no logging level checked) with the level
        * set to <code>SEVERE</code>.
        * <p>
        * Since a call stack will be created and logged, the location of the experimental feature will
        * already be readily apparent.
        *
        * @param    msg
        *           A description of the experimental feature.
        * @param    token
        *           A previously provided token value that uniquely distinguishes the caller
        *           or <code>null</code> if this is the first call.
        *
        * @return   The given token if it is a valid previously provided token.  Otherwise a newly
        *           created token will be associated with the caller and returned.
        */
       public static Object experimental(String msg, Object token)
       {
          synchronized (UnimplementedFeature.class)
          {
             if (token == null || !tokens.contains(token))
             {
                token = new Object();
                tokens.add(token);
                experimental(msg);
             }
          }

          return token;
       }

       /**
        * Log the given message along with the a stack trace and a prefix that indicates that
        * a feature was used at runtime for which the runtime support has a TODO remaining. The
        * logging is unconditionally done (there is no logging level checked) with the level set
        * to <code>SEVERE</code>.
        * <p>
        * Since a call stack will be created and logged, the location of the TODO will already be
        * readily apparent.
        *
        * @param    msg
        *           A description of the missing feature.
        */
       public static void todo(String msg)
       {
          log(Level.SEVERE, "TODO", msg);
       }

       /**
        * Conditionally log the given message if the caller's provided token is <code>null</code>
        * or if it was not a valid token.  Subsequent calls to this method will silently return
        * without logging if the given token already exists in the set of valid tokens. Any log entry
        * will have the given text along with the a stack trace and a prefix that indicates that
        * a feature was used at runtime for which the runtime support has a TODO remaining. The
        * logging is unconditionally done (there is no logging level checked) with the level set
        * to <code>SEVERE</code>.
        * <p>
        * Since a call stack will be created and logged, the location of the TODO will already be
        * readily apparent.
        *
        * @param    msg
        *           A description of the missing feature.
        * @param    token
        *           A previously provided token value that uniquely distinguishes the caller
        *           or <code>null</code> if this is the first call.
        *
        * @return   The given token if it is a valid previously provided token.  Otherwise a newly
        *           created token will be associated with the caller and returned.
        */
       public static Object todo(String msg, Object token)
       {
          synchronized (UnimplementedFeature.class)
          {
             if (token == null || !tokens.contains(token))
             {
                token = new Object();
                tokens.add(token);
                todo(msg);
             }
          }

          return token;
       }

       /**
        * Log the given message along with a given stack trace and a prefix that indicates that
        * a feature was used at runtime for which the runtime support is never expected to be
        * provided. The logging is unconditionally done (there is no logging level checked) with
        * the level set to <code>SEVERE</code>.
        * <p>
        * Since a call stack will be created and logged, the location of the unsupported feature will
        * already be readily apparent.
        *
        * @param    msg
        *           A description of the unsupported feature.
        */
       public static void unsupported(String msg)
       {
          log(Level.SEVERE, "UNSUPPORTED", msg);
       }

       /**
        * Conditionally log the given message if the caller's provided token is <code>null</code>
        * or if it was not a valid token.  Subsequent calls to this method will silently return
        * without logging if the given token already exists in the set of valid tokens. Any log entry
        * will have the given text along with the a stack trace and a prefix that indicates that
        * a feature was used at runtime for which the runtime support is never expected to be
        * provided. The logging is unconditionally done (there is no logging level checked) with the
        * level set to <code>SEVERE</code>.
        * <p>
        * Since a call stack will be created and logged, the location of the unsupported feature will
        * already be readily apparent.
        *
        * @param    msg
        *           A description of the unsupported feature.
        * @param    token
        *           A previously provided token value that uniquely distinguishes the caller
        *           or <code>null</code> if this is the first call.
        *
        * @return   The given token if it is a valid previously provided token.  Otherwise a newly
        *           created token will be associated with the caller and returned.
        */
       public static Object unsupported(String msg, Object token)
       {
          synchronized (UnimplementedFeature.class)
          {
             if (token == null || !tokens.contains(token))
             {
                token = new Object();
                tokens.add(token);
                unsupported(msg);
             }
          }

          return token;
       }

       /**
        * Logging helper method that logs a message at the given level.
        *
        * @param    level
        *           Logging level.
        * @param    code
        *           Describes the type of unimplemented feature.
        * @param    msg
        *           The caller-defined message.
        */
       private static void log(Level level, String code, String msg)
       {
          // the code is passed to logp but not honored, so we add it explicitly to the text
          String txt = String.format("%s: %s", code, msg);

          if (logger.isLoggable(Level.FINE))
          {
             // include an exception to log the stack trace if debug-level logging enabled
             logger.logp(level, "UnimplementedFeature", code, txt, new RuntimeException());

             return;
          }

          // log without a stack trace
          logger.logp(level, "UnimplementedFeature", code, txt);
       }
    }