diff options
author | Nasser Grainawi <quic_nasserg@quicinc.com> | 2021-02-26 15:49:06 -0700 |
---|---|---|
committer | Matthias Sohn <matthias.sohn@sap.com> | 2021-03-04 22:51:20 +0100 |
commit | c57b2935cd8bf160cb81ef86205ea0c5ea36da98 (patch) | |
tree | 3826b1056787b58db189edf3e2dc8ae7fc89b2d3 | |
parent | 6167641834e28f8ad322f8fde60866b339bfb7fe (diff) | |
download | jgit-c57b2935cd8bf160cb81ef86205ea0c5ea36da98.tar.gz jgit-c57b2935cd8bf160cb81ef86205ea0c5ea36da98.zip |
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>
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackDirectory.java | 15 | ||||
-rw-r--r-- | org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackExt.java | 60 |
2 files changed, 19 insertions, 56 deletions
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackDirectory.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackDirectory.java index 5f30970233..73745d8c64 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackDirectory.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackDirectory.java @@ -22,6 +22,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; +import java.util.EnumMap; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -512,13 +513,15 @@ class PackDirectory { for (String name : nameList) { try { PackFile pack = new PackFile(directory, name); - Map<PackExt, PackFile> packByExt = packFilesByExtById - .get(pack.getId()); - if (packByExt == null) { - packByExt = new HashMap<>(PackExt.values().length); - packFilesByExtById.put(pack.getId(), packByExt); + if (pack.getPackExt() != null) { + Map<PackExt, PackFile> packByExt = packFilesByExtById + .get(pack.getId()); + if (packByExt == null) { + packByExt = new EnumMap<>(PackExt.class); + packFilesByExtById.put(pack.getId(), packByExt); + } + packByExt.put(pack.getPackExt(), pack); } - packByExt.put(pack.getPackExt(), pack); } catch (IllegalArgumentException e) { continue; } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackExt.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackExt.java index bedc6939c8..6fb775da8d 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackExt.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackExt.java @@ -13,66 +13,26 @@ package org.eclipse.jgit.internal.storage.pack; /** * A pack file extension. */ -public class PackExt { - private static volatile PackExt[] VALUES = new PackExt[] {}; - +public enum PackExt { /** A pack file extension. */ - public static final PackExt PACK = newPackExt("pack"); //$NON-NLS-1$ + PACK("pack"), //$NON-NLS-1$ /** 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. */ - public static final PackExt KEEP = newPackExt("keep"); //$NON-NLS-1$ + KEEP("keep"), //$NON-NLS-1$ /** 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. */ - public static final PackExt REFTABLE = newPackExt("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; - } + REFTABLE("ref"); //$NON-NLS-1$ private final String ext; - private final int pos; - - private PackExt(String ext, int pos) { + private PackExt(String ext) { this.ext = ext; - this.pos = pos; } /** @@ -85,12 +45,12 @@ public class PackExt { } /** - * 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() { - return pos; + return ordinal(); } /** |