View Javadoc
1   /*
2    * Copyright (C) 2010, 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.file;
12  
13  import java.io.IOException;
14  
15  import org.eclipse.jgit.internal.storage.pack.StoredObjectRepresentation;
16  import org.eclipse.jgit.lib.ObjectId;
17  
18  class LocalObjectRepresentation extends StoredObjectRepresentation {
19  	static LocalObjectRepresentation newWhole(PackFile f, long p, long length) {
20  		LocalObjectRepresentation r = new LocalObjectRepresentation() {
21  			@Override
22  			public int getFormat() {
23  				return PACK_WHOLE;
24  			}
25  		};
26  		r.pack = f;
27  		r.offset = p;
28  		r.length = length;
29  		return r;
30  	}
31  
32  	static LocalObjectRepresentation newDelta(PackFile f, long p, long n,
33  			ObjectId base) {
34  		LocalObjectRepresentation r = new Delta();
35  		r.pack = f;
36  		r.offset = p;
37  		r.length = n;
38  		r.baseId = base;
39  		return r;
40  	}
41  
42  	static LocalObjectRepresentation newDelta(PackFile f, long p, long n,
43  			long base) {
44  		LocalObjectRepresentation r = new Delta();
45  		r.pack = f;
46  		r.offset = p;
47  		r.length = n;
48  		r.baseOffset = base;
49  		return r;
50  	}
51  
52  	PackFile pack;
53  
54  	long offset;
55  
56  	long length;
57  
58  	private long baseOffset;
59  
60  	private ObjectId baseId;
61  
62  	/** {@inheritDoc} */
63  	@Override
64  	public int getWeight() {
65  		return (int) Math.min(length, Integer.MAX_VALUE);
66  	}
67  
68  	/** {@inheritDoc} */
69  	@Override
70  	public ObjectId getDeltaBase() {
71  		if (baseId == null && getFormat() == PACK_DELTA) {
72  			try {
73  				baseId = pack.findObjectForOffset(baseOffset);
74  			} catch (IOException error) {
75  				return null;
76  			}
77  		}
78  		return baseId;
79  	}
80  
81  	private static final class Delta extends LocalObjectRepresentation {
82  		@Override
83  		public int getFormat() {
84  			return PACK_DELTA;
85  		}
86  	}
87  }