ObjectId.java

  1. /*
  2.  * Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  3.  * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  4.  * and other copyright owners as documented in the project's IP log.
  5.  *
  6.  * This program and the accompanying materials are made available
  7.  * under the terms of the Eclipse Distribution License v1.0 which
  8.  * accompanies this distribution, is reproduced below, and is
  9.  * available at http://www.eclipse.org/org/documents/edl-v10.php
  10.  *
  11.  * All rights reserved.
  12.  *
  13.  * Redistribution and use in source and binary forms, with or
  14.  * without modification, are permitted provided that the following
  15.  * conditions are met:
  16.  *
  17.  * - Redistributions of source code must retain the above copyright
  18.  *   notice, this list of conditions and the following disclaimer.
  19.  *
  20.  * - Redistributions in binary form must reproduce the above
  21.  *   copyright notice, this list of conditions and the following
  22.  *   disclaimer in the documentation and/or other materials provided
  23.  *   with the distribution.
  24.  *
  25.  * - Neither the name of the Eclipse Foundation, Inc. nor the
  26.  *   names of its contributors may be used to endorse or promote
  27.  *   products derived from this software without specific prior
  28.  *   written permission.
  29.  *
  30.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  31.  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  32.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  33.  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  34.  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  35.  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  36.  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  37.  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  38.  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  39.  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  40.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  41.  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  42.  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  43.  */

  44. package org.eclipse.jgit.lib;

  45. import java.io.IOException;
  46. import java.io.ObjectInputStream;
  47. import java.io.ObjectOutputStream;
  48. import java.io.Serializable;

  49. import org.eclipse.jgit.annotations.Nullable;
  50. import org.eclipse.jgit.errors.InvalidObjectIdException;
  51. import org.eclipse.jgit.util.NB;
  52. import org.eclipse.jgit.util.RawParseUtils;

  53. /**
  54.  * A SHA-1 abstraction.
  55.  */
  56. public class ObjectId extends AnyObjectId implements Serializable {
  57.     private static final long serialVersionUID = 1L;

  58.     private static final ObjectId ZEROID;

  59.     private static final String ZEROID_STR;

  60.     static {
  61.         ZEROID = new ObjectId(0, 0, 0, 0, 0);
  62.         ZEROID_STR = ZEROID.name();
  63.     }

  64.     /**
  65.      * Get the special all-null ObjectId.
  66.      *
  67.      * @return the all-null ObjectId, often used to stand-in for no object.
  68.      */
  69.     public static final ObjectId zeroId() {
  70.         return ZEROID;
  71.     }

  72.     /**
  73.      * Test a string of characters to verify it is a hex format.
  74.      * <p>
  75.      * If true the string can be parsed with {@link #fromString(String)}.
  76.      *
  77.      * @param id
  78.      *            the string to test.
  79.      * @return true if the string can converted into an ObjectId.
  80.      */
  81.     public static final boolean isId(@Nullable String id) {
  82.         if (id == null) {
  83.             return false;
  84.         }
  85.         if (id.length() != Constants.OBJECT_ID_STRING_LENGTH)
  86.             return false;
  87.         try {
  88.             for (int i = 0; i < Constants.OBJECT_ID_STRING_LENGTH; i++) {
  89.                 RawParseUtils.parseHexInt4((byte) id.charAt(i));
  90.             }
  91.             return true;
  92.         } catch (ArrayIndexOutOfBoundsException e) {
  93.             return false;
  94.         }
  95.     }

  96.     /**
  97.      * Convert an ObjectId into a hex string representation.
  98.      *
  99.      * @param i
  100.      *            the id to convert. May be null.
  101.      * @return the hex string conversion of this id's content.
  102.      */
  103.     public static final String toString(ObjectId i) {
  104.         return i != null ? i.name() : ZEROID_STR;
  105.     }

  106.     /**
  107.      * Compare two object identifier byte sequences for equality.
  108.      *
  109.      * @param firstBuffer
  110.      *            the first buffer to compare against. Must have at least 20
  111.      *            bytes from position fi through the end of the buffer.
  112.      * @param fi
  113.      *            first offset within firstBuffer to begin testing.
  114.      * @param secondBuffer
  115.      *            the second buffer to compare against. Must have at least 20
  116.      *            bytes from position si through the end of the buffer.
  117.      * @param si
  118.      *            first offset within secondBuffer to begin testing.
  119.      * @return true if the two identifiers are the same.
  120.      */
  121.     public static boolean equals(final byte[] firstBuffer, final int fi,
  122.             final byte[] secondBuffer, final int si) {
  123.         return firstBuffer[fi] == secondBuffer[si]
  124.                 && firstBuffer[fi + 1] == secondBuffer[si + 1]
  125.                 && firstBuffer[fi + 2] == secondBuffer[si + 2]
  126.                 && firstBuffer[fi + 3] == secondBuffer[si + 3]
  127.                 && firstBuffer[fi + 4] == secondBuffer[si + 4]
  128.                 && firstBuffer[fi + 5] == secondBuffer[si + 5]
  129.                 && firstBuffer[fi + 6] == secondBuffer[si + 6]
  130.                 && firstBuffer[fi + 7] == secondBuffer[si + 7]
  131.                 && firstBuffer[fi + 8] == secondBuffer[si + 8]
  132.                 && firstBuffer[fi + 9] == secondBuffer[si + 9]
  133.                 && firstBuffer[fi + 10] == secondBuffer[si + 10]
  134.                 && firstBuffer[fi + 11] == secondBuffer[si + 11]
  135.                 && firstBuffer[fi + 12] == secondBuffer[si + 12]
  136.                 && firstBuffer[fi + 13] == secondBuffer[si + 13]
  137.                 && firstBuffer[fi + 14] == secondBuffer[si + 14]
  138.                 && firstBuffer[fi + 15] == secondBuffer[si + 15]
  139.                 && firstBuffer[fi + 16] == secondBuffer[si + 16]
  140.                 && firstBuffer[fi + 17] == secondBuffer[si + 17]
  141.                 && firstBuffer[fi + 18] == secondBuffer[si + 18]
  142.                 && firstBuffer[fi + 19] == secondBuffer[si + 19];
  143.     }

  144.     /**
  145.      * Convert an ObjectId from raw binary representation.
  146.      *
  147.      * @param bs
  148.      *            the raw byte buffer to read from. At least 20 bytes must be
  149.      *            available within this byte array.
  150.      * @return the converted object id.
  151.      */
  152.     public static final ObjectId fromRaw(byte[] bs) {
  153.         return fromRaw(bs, 0);
  154.     }

  155.     /**
  156.      * Convert an ObjectId from raw binary representation.
  157.      *
  158.      * @param bs
  159.      *            the raw byte buffer to read from. At least 20 bytes after p
  160.      *            must be available within this byte array.
  161.      * @param p
  162.      *            position to read the first byte of data from.
  163.      * @return the converted object id.
  164.      */
  165.     public static final ObjectId fromRaw(byte[] bs, int p) {
  166.         final int a = NB.decodeInt32(bs, p);
  167.         final int b = NB.decodeInt32(bs, p + 4);
  168.         final int c = NB.decodeInt32(bs, p + 8);
  169.         final int d = NB.decodeInt32(bs, p + 12);
  170.         final int e = NB.decodeInt32(bs, p + 16);
  171.         return new ObjectId(a, b, c, d, e);
  172.     }

  173.     /**
  174.      * Convert an ObjectId from raw binary representation.
  175.      *
  176.      * @param is
  177.      *            the raw integers buffer to read from. At least 5 integers must
  178.      *            be available within this int array.
  179.      * @return the converted object id.
  180.      */
  181.     public static final ObjectId fromRaw(int[] is) {
  182.         return fromRaw(is, 0);
  183.     }

  184.     /**
  185.      * Convert an ObjectId from raw binary representation.
  186.      *
  187.      * @param is
  188.      *            the raw integers buffer to read from. At least 5 integers
  189.      *            after p must be available within this int array.
  190.      * @param p
  191.      *            position to read the first integer of data from.
  192.      * @return the converted object id.
  193.      */
  194.     public static final ObjectId fromRaw(int[] is, int p) {
  195.         return new ObjectId(is[p], is[p + 1], is[p + 2], is[p + 3], is[p + 4]);
  196.     }

  197.     /**
  198.      * Convert an ObjectId from hex characters (US-ASCII).
  199.      *
  200.      * @param buf
  201.      *            the US-ASCII buffer to read from. At least 40 bytes after
  202.      *            offset must be available within this byte array.
  203.      * @param offset
  204.      *            position to read the first character from.
  205.      * @return the converted object id.
  206.      */
  207.     public static final ObjectId fromString(byte[] buf, int offset) {
  208.         return fromHexString(buf, offset);
  209.     }

  210.     /**
  211.      * Convert an ObjectId from hex characters.
  212.      *
  213.      * @param str
  214.      *            the string to read from. Must be 40 characters long.
  215.      * @return the converted object id.
  216.      */
  217.     public static ObjectId fromString(String str) {
  218.         if (str.length() != Constants.OBJECT_ID_STRING_LENGTH) {
  219.             throw new InvalidObjectIdException(str);
  220.         }
  221.         return fromHexString(Constants.encodeASCII(str), 0);
  222.     }

  223.     private static final ObjectId fromHexString(byte[] bs, int p) {
  224.         try {
  225.             final int a = RawParseUtils.parseHexInt32(bs, p);
  226.             final int b = RawParseUtils.parseHexInt32(bs, p + 8);
  227.             final int c = RawParseUtils.parseHexInt32(bs, p + 16);
  228.             final int d = RawParseUtils.parseHexInt32(bs, p + 24);
  229.             final int e = RawParseUtils.parseHexInt32(bs, p + 32);
  230.             return new ObjectId(a, b, c, d, e);
  231.         } catch (ArrayIndexOutOfBoundsException e1) {
  232.             throw new InvalidObjectIdException(bs, p,
  233.                     Constants.OBJECT_ID_STRING_LENGTH);
  234.         }
  235.     }

  236.     /**
  237.      * Construct an ObjectId from 160 bits provided in 5 words.
  238.      *
  239.      * @param new_1
  240.      *            an int
  241.      * @param new_2
  242.      *            an int
  243.      * @param new_3
  244.      *            an int
  245.      * @param new_4
  246.      *            an int
  247.      * @param new_5
  248.      *            an int
  249.      * @since 4.7
  250.      */
  251.     public ObjectId(int new_1, int new_2, int new_3, int new_4, int new_5) {
  252.         w1 = new_1;
  253.         w2 = new_2;
  254.         w3 = new_3;
  255.         w4 = new_4;
  256.         w5 = new_5;
  257.     }

  258.     /**
  259.      * Initialize this instance by copying another existing ObjectId.
  260.      * <p>
  261.      * This constructor is mostly useful for subclasses who want to extend an
  262.      * ObjectId with more properties, but initialize from an existing ObjectId
  263.      * instance acquired by other means.
  264.      *
  265.      * @param src
  266.      *            another already parsed ObjectId to copy the value out of.
  267.      */
  268.     protected ObjectId(AnyObjectId src) {
  269.         w1 = src.w1;
  270.         w2 = src.w2;
  271.         w3 = src.w3;
  272.         w4 = src.w4;
  273.         w5 = src.w5;
  274.     }

  275.     /** {@inheritDoc} */
  276.     @Override
  277.     public ObjectId toObjectId() {
  278.         return this;
  279.     }

  280.     private void writeObject(ObjectOutputStream os) throws IOException {
  281.         os.writeInt(w1);
  282.         os.writeInt(w2);
  283.         os.writeInt(w3);
  284.         os.writeInt(w4);
  285.         os.writeInt(w5);
  286.     }

  287.     private void readObject(ObjectInputStream ois) throws IOException {
  288.         w1 = ois.readInt();
  289.         w2 = ois.readInt();
  290.         w3 = ois.readInt();
  291.         w4 = ois.readInt();
  292.         w5 = ois.readInt();
  293.     }
  294. }