PackExt: Convert to Enum

This class already looked very much like an Enum, but wasn't one.

As an Enum, we can use PackExt in EnumMaps and EnumSets. Convert the
Map key usage in PackDirectory to an EnumMap.

Change-Id: Ice097fd468a05805f914e6862fbd1d96ec8c45d1
Signed-off-by: Nasser Grainawi <quic_nasserg@quicinc.com>
This commit is contained in:
Nasser Grainawi 2021-02-26 15:49:06 -07:00 committed by Matthias Sohn
parent 6167641834
commit c57b2935cd
2 changed files with 19 additions and 56 deletions

View File

@ -22,6 +22,7 @@
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -512,13 +513,15 @@ private Map<String, Map<PackExt, PackFile>> getPackFilesByExtById() {
for (String name : nameList) { for (String name : nameList) {
try { try {
PackFile pack = new PackFile(directory, name); PackFile pack = new PackFile(directory, name);
if (pack.getPackExt() != null) {
Map<PackExt, PackFile> packByExt = packFilesByExtById Map<PackExt, PackFile> packByExt = packFilesByExtById
.get(pack.getId()); .get(pack.getId());
if (packByExt == null) { if (packByExt == null) {
packByExt = new HashMap<>(PackExt.values().length); packByExt = new EnumMap<>(PackExt.class);
packFilesByExtById.put(pack.getId(), packByExt); packFilesByExtById.put(pack.getId(), packByExt);
} }
packByExt.put(pack.getPackExt(), pack); packByExt.put(pack.getPackExt(), pack);
}
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
continue; continue;
} }

View File

@ -13,66 +13,26 @@
/** /**
* A pack file extension. * A pack file extension.
*/ */
public class PackExt { public enum PackExt {
private static volatile PackExt[] VALUES = new PackExt[] {};
/** A pack file extension. */ /** A pack file extension. */
public static final PackExt PACK = newPackExt("pack"); //$NON-NLS-1$ PACK("pack"), //$NON-NLS-1$
/** A pack index file extension. */ /** A pack index file extension. */
public static final PackExt INDEX = newPackExt("idx"); //$NON-NLS-1$ INDEX("idx"), //$NON-NLS-1$
/** A keep pack file extension. */ /** A keep pack file extension. */
public static final PackExt KEEP = newPackExt("keep"); //$NON-NLS-1$ KEEP("keep"), //$NON-NLS-1$
/** A pack bitmap index file extension. */ /** A pack bitmap index file extension. */
public static final PackExt BITMAP_INDEX = newPackExt("bitmap"); //$NON-NLS-1$ BITMAP_INDEX("bitmap"), //$NON-NLS-1$
/** A reftable file. */ /** A reftable file. */
public static final PackExt REFTABLE = newPackExt("ref"); //$NON-NLS-1$ REFTABLE("ref"); //$NON-NLS-1$
/**
* Get all of the PackExt values.
*
* @return all of the PackExt values.
*/
public static PackExt[] values() {
return VALUES;
}
/**
* Returns a PackExt for the file extension and registers it in the values
* array.
*
* @param ext
* the file extension.
* @return the PackExt for the ext
*/
public static synchronized PackExt newPackExt(String ext) {
PackExt[] dst = new PackExt[VALUES.length + 1];
for (int i = 0; i < VALUES.length; i++) {
PackExt packExt = VALUES[i];
if (packExt.getExtension().equals(ext))
return packExt;
dst[i] = packExt;
}
if (VALUES.length >= 32)
throw new IllegalStateException(
"maximum number of pack extensions exceeded"); //$NON-NLS-1$
PackExt value = new PackExt(ext, VALUES.length);
dst[VALUES.length] = value;
VALUES = dst;
return value;
}
private final String ext; private final String ext;
private final int pos; private PackExt(String ext) {
private PackExt(String ext, int pos) {
this.ext = ext; this.ext = ext;
this.pos = pos;
} }
/** /**
@ -85,12 +45,12 @@ public String getExtension() {
} }
/** /**
* Get the position of the extension in the values array. * Get the position of the extension in the enum declaration.
* *
* @return the position of the extension in the values array. * @return the position of the extension in the enum declaration.
*/ */
public int getPosition() { public int getPosition() {
return pos; return ordinal();
} }
/** /**