View Javadoc
1   /*
2    * Copyright (C) 2021, Thomas Wolf <thomas.wolf@paranor.ch> 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  package org.eclipse.jgit.api.errors;
11  
12  import java.text.MessageFormat;
13  
14  import org.eclipse.jgit.internal.JGitText;
15  import org.eclipse.jgit.lib.Constants;
16  import org.eclipse.jgit.lib.ObjectId;
17  
18  /**
19   * A given object is not of an expected object type.
20   *
21   * @since 5.11
22   */
23  public class WrongObjectTypeException extends GitAPIException {
24  
25  	private static final long serialVersionUID = 1L;
26  
27  	private String name;
28  
29  	private int type;
30  
31  	/**
32  	 * Construct a {@link WrongObjectTypeException} for the specified object id,
33  	 * giving the expected type.
34  	 *
35  	 * @param id
36  	 *            {@link ObjectId} of the object with the unexpected type
37  	 * @param type
38  	 *            expected object type code; see
39  	 *            {@link Constants}{@code .OBJ_*}.
40  	 */
41  	public WrongObjectTypeException(ObjectId id, int type) {
42  		super(MessageFormat.format(JGitText.get().objectIsNotA, id.name(),
43  				Constants.typeString(type)));
44  		this.name = id.name();
45  		this.type = type;
46  	}
47  
48  	/**
49  	 * Retrieves the name (SHA-1) of the object.
50  	 *
51  	 * @return the name
52  	 */
53  	public String getObjectId() {
54  		return name;
55  	}
56  
57  	/**
58  	 * Retrieves the expected type code. See {@link Constants}{@code .OBJ_*}.
59  	 *
60  	 * @return the type code
61  	 */
62  	public int getExpectedType() {
63  		return type;
64  	}
65  }