View Javadoc
1   /*
2    * Copyright (C) 2013, Google Inc. and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.internal.storage.pack;
12  
13  /**
14   * A pack file extension.
15   */
16  public class PackExt {
17  	private static volatile PackExt/storage/pack/PackExt.html#PackExt">PackExt[] VALUES = new PackExt[] {};
18  
19  	/** A pack file extension. */
20  	public static final PackExt PACK = newPackExt("pack"); //$NON-NLS-1$
21  
22  	/** A pack index file extension. */
23  	public static final PackExt INDEX = newPackExt("idx"); //$NON-NLS-1$
24  
25  	/** A keep pack file extension. */
26  	public static final PackExt KEEP = newPackExt("keep"); //$NON-NLS-1$
27  
28  	/** A pack bitmap index file extension. */
29  	public static final PackExt BITMAP_INDEX = newPackExt("bitmap"); //$NON-NLS-1$
30  
31  	/** A reftable file. */
32  	public static final PackExt REFTABLE = newPackExt("ref"); //$NON-NLS-1$
33  
34  	/**
35  	 * Get all of the PackExt values.
36  	 *
37  	 * @return all of the PackExt values.
38  	 */
39  	public static PackExt[] values() {
40  		return VALUES;
41  	}
42  
43  	/**
44  	 * Returns a PackExt for the file extension and registers it in the values
45  	 * array.
46  	 *
47  	 * @param ext
48  	 *            the file extension.
49  	 * @return the PackExt for the ext
50  	 */
51  	public static synchronized PackExt newPackExt(String ext) {
52  		PackExt[] dst = new PackExt[VALUES.length + 1];
53  		for (int i = 0; i < VALUES.length; i++) {
54  			PackExt packExt = VALUES[i];
55  			if (packExt.getExtension().equals(ext))
56  				return packExt;
57  			dst[i] = packExt;
58  		}
59  		if (VALUES.length >= 32)
60  			throw new IllegalStateException(
61  					"maximum number of pack extensions exceeded"); //$NON-NLS-1$
62  
63  		PackExt value = new PackExt(ext, VALUES.length);
64  		dst[VALUES.length] = value;
65  		VALUES = dst;
66  		return value;
67  	}
68  
69  	private final String ext;
70  
71  	private final int pos;
72  
73  	private PackExt(String ext, int pos) {
74  		this.ext = ext;
75  		this.pos = pos;
76  	}
77  
78  	/**
79  	 * Get the file extension.
80  	 *
81  	 * @return the file extension.
82  	 */
83  	public String getExtension() {
84  		return ext;
85  	}
86  
87  	/**
88  	 * Get the position of the extension in the values array.
89  	 *
90  	 * @return the position of the extension in the values array.
91  	 */
92  	public int getPosition() {
93  		return pos;
94  	}
95  
96  	/**
97  	 * Get the bit mask of the extension e.g {@code 1 << getPosition()}.
98  	 *
99  	 * @return the bit mask of the extension e.g {@code 1 << getPosition()}.
100 	 */
101 	public int getBit() {
102 		return 1 << getPosition();
103 	}
104 
105 	/** {@inheritDoc} */
106 	@Override
107 	public String toString() {
108 		return String.format("PackExt[%s, bit=0x%s]", getExtension(), //$NON-NLS-1$
109 				Integer.toHexString(getBit()));
110 	}
111 }