1

Is there a more efficient way to achieve this result. I essentially want to look at the Section with the longest getContextPath() value first.

enum Section {
    Provider("Provider", "com.app.provider"),
    Container("Container", "com.app");

    /**
     * The key for this Section.
     */
    private String key;

    /**
     * The context path for this Section.
     */
    private String contextPath;

    /**
     * Create a new Section.
     *
     * @param key         The Key for this section.
     * @param contextPath The Context Path for this section.
     */
    Section(String key, String contextPath) {
        this.key = key;
        this.contextPath = contextPath;
    }

    /**
     * Retrieve the String representation of this Section.
     *
     * @return The String value.
     */
    @SuppressWarnings("NullableProblems")
    @Override
    public String toString() {
        return this.key;
    }

    /**
     * Retrieve the Context Path for this Section.
     *
     * @return The Context Path.
     */
    public String getContextPath()
    {
        return this.contextPath;
    }
}

// Keep in mind that these may be dynamically loaded, and won't always be
// statically loaded like this.
private static final Map<Section, String> sectionLoggerIds = new HashMap<>() {{ 
    put(Section.Container, "some.id");
    put(Section.Provider, "some.other.id");
}};

/**
 * Retrieve a Logger based on a Context.
 *
 * @param context The Context.
 * @return The Logger
 */
public static Logger logger(Context context)  {
    // Create a TreeSet for sorting the values, and set it up
    // to sort them longest first.
    Set<Section> sectionSet = new TreeSet<>(new Comparator<Section>() {
        @Override
        public int compare(Section section1, Section section2) {
            return Integer.compare(
                section2.getContextPath().length(), 
                section1.getContextPath().length()
            );
        }
    });

    // Add all of the relevant Section objects.
    sectionSet.addAll(sectionLoggerIds.keySet());

    // Search the Sections, starting with the longest
    // contextPath first.
    for(Section section : sectionSet) {
        if (context.getClass().getName().contains(section.getContextPath())) {
            return getLogger(sectionLoggerIds.get(section));
        }
    }

    return null;
}
Nathan F.
  • 3,250
  • 3
  • 35
  • 69
  • Not quite clear as to what your question is. Are you asking if reversing the order of `section2` and `section1` in the `compare` method is the appropriate way to get the `TreeSet` naturally ordered by longest string first? If so, I suggest you ask that more explicitly. And I suggest commenting your code to explain that logic. – Basil Bourque May 30 '19 at 16:31
  • Possible related: [*Treeset to order elements in descending order*](https://stackoverflow.com/q/1090969/642706) – Basil Bourque May 30 '19 at 16:32
  • @Basil Borque I'm asking if this is the most efficient way to achieve the result that the `logger` function is achieving. – Nathan F. May 30 '19 at 16:32
  • You are sorting **two** values. Why do you care about efficiency? I don't think you can do *anything* so bad that you'll be able to notice any performance difference. – Andreas May 30 '19 at 16:33
  • 1
    What does "efficient" mean to you *specifically*? – Basil Bourque May 30 '19 at 16:33
  • If "efficient" is about amount of code, then you could write `new TreeSet<>(Comparator.comparing((Section s) -> s.getContextPath().length()).reversed())` or `new TreeSet<>((s1, s2) -> Integer.compare(s2.getContextPath().length(), s1.getContextPath().length()))` – Andreas May 30 '19 at 16:37
  • I put a comment in saying `Keep in mind that these may be dynamically loaded, and won't always be statically loaded like this.`. The `sectionLoggerIds` Set can have any number of entries, and it can get very large in the future. The final implementation likely won't be using an enum, and instead will be using a class that will dynamically allocate any number of objects at run time. Since this is called each time a log message is sent out, I want it to be as efficient as possible. So, i'm trying to figure out if this is the most efficient way of doing so, or if there is a more efficient way. – Nathan F. May 30 '19 at 16:51

0 Answers0