Skip to content

Commit

Permalink
PopupMenuBuilder: implement as a Map instead of a List
Browse files Browse the repository at this point in the history
  • Loading branch information
stephengold committed Jun 24, 2022
1 parent b8e0fc3 commit 7f5d272
Showing 1 changed file with 21 additions and 22 deletions.
43 changes: 21 additions & 22 deletions nifty/src/main/java/jme3utilities/nifty/PopupMenuBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
*/
package jme3utilities.nifty;

import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import jme3utilities.Validate;

Expand All @@ -49,13 +49,9 @@ public class PopupMenuBuilder {
// fields

/**
* list of menu icon asset paths TODO implement as a Map instead of a List
* map menu items to icon asset paths
*/
final protected List<String> iconAssetPaths = new ArrayList<>(40);
/**
* list of menu items
*/
final protected List<String> items = new ArrayList<>(40);
final protected Map<String, String> itemMap = new HashMap<>(40);
// *************************************************************************
// new methods exposed

Expand All @@ -66,9 +62,9 @@ public class PopupMenuBuilder {
*/
public void add(String item) {
Validate.nonEmpty(item, "item");
assert !itemMap.containsKey(item) : item;

items.add(item);
iconAssetPaths.add(null);
itemMap.put(item, null);
}

/**
Expand All @@ -79,9 +75,9 @@ public void add(String item) {
*/
public void add(String item, String iconAssetPath) {
Validate.nonEmpty(item, "item");
assert !itemMap.containsKey(item) : item;

items.add(item);
iconAssetPaths.add(iconAssetPath);
itemMap.put(item, iconAssetPath);
}

/**
Expand All @@ -103,10 +99,12 @@ public void addAll(Iterable<String> items) {
* @return a new array
*/
public String[] copyIconAssetPaths() {
int numIcons = iconAssetPaths.size();
int numIcons = itemMap.size();
String[] result = new String[numIcons];
for (int i = 0; i < numIcons; i++) {
result[i] = iconAssetPaths.get(i);
int i = 0;
for (String icontAssetPath : itemMap.values()) {
result[i] = icontAssetPath;
++i;
}

return result;
Expand All @@ -118,10 +116,12 @@ public String[] copyIconAssetPaths() {
* @return a new array
*/
public String[] copyItems() {
int numItems = items.size();
int numItems = itemMap.size();
String[] result = new String[numItems];
for (int i = 0; i < numItems; i++) {
result[i] = items.get(i);
int i = 0;
for (String item : itemMap.keySet()) {
result[i] = item;
++i;
}

return result;
Expand All @@ -135,7 +135,7 @@ public String[] copyItems() {
*/
public boolean hasItem(String item) {
Validate.nonEmpty(item, "item");
boolean result = items.contains(item);
boolean result = itemMap.containsKey(item);
return result;
}

Expand All @@ -145,15 +145,14 @@ public boolean hasItem(String item) {
* @return true if empty, otherwise false
*/
public boolean isEmpty() {
boolean result = items.isEmpty();
boolean result = itemMap.isEmpty();
return result;
}

/**
* Remove everything from the menu and start over.
*/
public void reset() {
items.clear();
iconAssetPaths.clear();
itemMap.clear();
}
}

0 comments on commit 7f5d272

Please sign in to comment.