Browse Source

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>
tags/v5.11.0.202103091610-r
Nasser Grainawi 3 years ago
parent
commit
c57b2935cd

+ 9
- 6
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/PackDirectory.java View File

@@ -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;
}

+ 10
- 50
org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/pack/PackExt.java View File

@@ -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();
}

/**

Loading…
Cancel
Save