We help IT Professionals succeed at work.
Get Started

improve performance and should be scalable

Javatechno
Javatechno asked
on
353 Views
Last Modified: 2013-12-29
I have written a code snippet, this code will be access from thousands of object and get method look up also will be done from these objects. I need help in improving performance of this code and should be scalable so that if more then 10000 objects are accessing it it should not effect performance
import java.util.Set;
import java.util.HashSet;

/**
 * Implementation of the WidgetCatalog interface.  Please see the interface
 * for the specification.
 */
public class WidgetCatalogImpl implements WidgetCatalog
{

    // using JDK 1.5
    Set<Widget> set = new HashSet<Widget>();


    public void add(Widget widget) {
        set.add(widget);
    }

   /**
    * Return the widget for the specified version of the named widget.  If
    * the exact flag is true then this will only return a widget for an
    * exact name/version match.  If the exact flag is false then this will
    * return the version with a matching major version but the highest
    * available minor number.
    *
    * @param name    the name of the widget
    * @param major   the major version
    * @param minor   the minor version
    * @param exact   whether we need an exact match
    */    
    public Widget get(String name,int major,int minor,boolean exact) {
       Widget widgetToReturn = null;
       if(exact) {
           Widget w = new Widget(name, major, minor);

           // for loop using JDK 1.5 version
           for(Widget wid : set) {
                if((w.getName().equals(wid.getName())) && (wid.getVersion()).equals(w.getVersion())) {
                    widgetToReturn = w;
                    break;
                } 
           }
       } else {
           Widget w = new Widget(name, major, minor);
           WidgetVersion widgetVersion = new WidgetVersion(major, minor);

           // for loop using JDK 1.5 version
           for(Widget wid : set) {
               WidgetVersion wv = wid.getVersion();
               if((w.getName().equals(wid.getName())) && major == wv.getMajor() && WidgetVersion.isCompatibleAndNewer(wv, widgetVersion)) {
                    widgetToReturn = wid;
               } else if((w.getName().equals(wid.getName())) && wv.equals(widgetVersion.getMajor(), widgetVersion.getMinor())) {
                    widgetToReturn = w;
               }
           }
       }
       return widgetToReturn;
   }

   /**
    * Return the widget for the specified version of the named widget.
    * This will return the newest version of the widget that is compatible,
    * meaning the major version is the same but the minor version is equal to
    * or higher than the desired version.
    *
    * @param name    the name of the widget
    * @param major   the major version
    * @param minor   the minor version
    */
    public Widget getBest(String name,int major,int minor) {

       // THIS METHOD IS NOT GETTING USED ANY WHERE
        return null;
    }
    
}

Open in new window

Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 10 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE