Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8352237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T08:54:29+00:00 2026-06-09T08:54:29+00:00

What type of process would I need in a new class to ‘read’ all

  • 0

What type of process would I need in a new class to ‘read’ all of the colors that are in this class below? I use the class to set colors on components and love it. But the time has come to allow my application to right-click on a panel and change the background color for example.

What would get me started in the correct direction code-wise in continuing to use these colors: ColorFactory.java

I was just starting to think about adding a new method to the class something like:

public Map<String, Color> getColorMapValues(){

    return colorMap;
    //
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T08:54:32+00:00Added an answer on June 9, 2026 at 8:54 am

    Since you have control over the source code in this case, it would be better to refactor the class to meet your needs than using reflection (which is not only expensive, but is prohibited depending on your security policy).

    The technique commonly used is the Holder pattern. See Joshua Bloch’s Effective Java (2nd Edition) Item 71 for more information. We can also avoid synchronization on reads by using a thread-safe, non blocking structure, namely java.util.concurrent.ConcurrentHashMap.

    public class ColorFactory {
        private static class ColorFactoryHolder {
            // creates on instantiation of ColorFactoryHolder
            // synchronization is baked into the JVM, but won't be created until
            // the class is used, see JLS 12.4.1
            static final ColorFactory instance = new ColorMap();
        }
        public static ColorFactory getInstance() { return ColorFactoryHolder.instance; }
        // concurrent hash map - all operations are thread safe
        Map<String,Color> colormap = new ConcurrentHashMap<String,Color>();
        private final Object lock = new Object();
        private ColorFactory() {
            colormap.add("blue",new Color(0,0,255));
            // rest of colors here
        }
        public Color getColor(String spec) {
            if(colormap.containsKey(spec)) return colormap.get(spec);
            // don't synchronize externally - Bloch et al, item 70
            synchronized(lock) { 
                // double check idiom - not broken, as map is thread safe
                if(colormap.containsKey(spec)) return colormap.get(spec);
                Color color = parse(spec); // parse method can be extracted from old code
                colormap.put(spec,color);
                return color;
            }
        }
        private static Color parse(String spec) {
            // parse the color spec here
        }    
    }
    

    In fact, because the parse operation is likely to be very, very fast (in comparison to the synchronization), we can do away with the synchronization altogether. So we might end up parsing the value multiple times – not really a big problem, since the result will be the same every time. See Bloch et al. Item 69 for more information.

    public class ColorFactory {
        private static class ColorFactoryHolder {
            // same as above, snipped for brevity
        }
        public static ColorFactory getInstance() { return ColorFactoryHolder.instance; }
        // requires ConcurrentMap reference to get putIfAbsent(K,V) method
        ConcurrentMap<String,Color> colormap = new ConcurrentHashMap<String,Color>();
        // private final Object lock = new Object(); - removed
        private ColorFactory() {
            colormap.add("blue",new Color(0,0,255));
            // rest of colors here
        }
        public Color getColor(String spec) {
            Color result = colormap.get(spec);
            if(result == null) {         
                result = parse(spec); // may parse multiple times, but still
                                      // cheaper than synchronization
                colormap.putIfAbsent(spec,result);            
            }
            return result
        }
        private static Color parse(String spec) {
            // parse the color spec here
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I execute the ftp.exe cmd through a C# System.Diagnostics.Process type. And I use the
I've created TFS project based on MSF for CMMI process template. For any type
The type of this function is T -> (T -> U) -> U .
What type of transaction management strategy we should use in Spring? Declarative or Programmatic?
System.Type contains an UnderlyingSystemType property. MSDN states that it: Indicates the type provided by
I have the following class and extension class (for this example): public class Person<T>
This is tricky to explain. We have a DataTable that contains a user configurable
This may be a basic question but I am pretty new to DDD. I
Background I have a few scripts that run as part of my build process
I am trying to use the following code to write out all processes started

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.