Replace inefficient new Long(long) constructor to silence FindBugs
[egit/imyousuf.git] / org.spearce.jgit / src / org / spearce / jgit / merge / MergeStrategy.java
blob5439e5cc3e4b5389df414da5c8390aa3f8112bfe
1 /*
2 * Copyright (C) 2008, Google Inc.
4 * All rights reserved.
6 * Redistribution and use in source and binary forms, with or
7 * without modification, are permitted provided that the following
8 * conditions are met:
10 * - Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * - Neither the name of the Git Development Community nor the
19 * names of its contributors may be used to endorse or promote
20 * products derived from this software without specific prior
21 * written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
24 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
25 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
26 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
33 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
35 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 package org.spearce.jgit.merge;
40 import java.util.HashMap;
42 import org.spearce.jgit.lib.Repository;
44 /**
45 * A method of combining two or more trees together to form an output tree.
46 * <p>
47 * Different strategies may employ different techniques for deciding which paths
48 * (and ObjectIds) to carry from the input trees into the final output tree.
50 public abstract class MergeStrategy {
51 /** Simple strategy that sets the output tree to the first input tree. */
52 public static final MergeStrategy OURS = new StrategyOneSided("ours", 0);
54 /** Simple strategy that sets the output tree to the second input tree. */
55 public static final MergeStrategy THEIRS = new StrategyOneSided("theirs", 1);
57 /** Simple strategy to merge paths, without simultaneous edits. */
58 public static final ThreeWayMergeStrategy SIMPLE_TWO_WAY_IN_CORE = StrategySimpleTwoWayInCore.INSTANCE;
60 private static final HashMap<String, MergeStrategy> STRATEGIES = new HashMap<String, MergeStrategy>();
62 static {
63 register(OURS);
64 register(THEIRS);
65 register(SIMPLE_TWO_WAY_IN_CORE);
68 /**
69 * Register a merge strategy so it can later be obtained by name.
71 * @param imp
72 * the strategy to register.
73 * @throws IllegalArgumentException
74 * a strategy by the same name has already been registered.
76 public static void register(final MergeStrategy imp) {
77 register(imp.getName(), imp);
80 /**
81 * Register a merge strategy so it can later be obtained by name.
83 * @param name
84 * name the strategy can be looked up under.
85 * @param imp
86 * the strategy to register.
87 * @throws IllegalArgumentException
88 * a strategy by the same name has already been registered.
90 public static synchronized void register(final String name,
91 final MergeStrategy imp) {
92 if (STRATEGIES.containsKey(name))
93 throw new IllegalArgumentException("Merge strategy \"" + name
94 + "\" already exists as a default strategy");
95 STRATEGIES.put(name, imp);
98 /**
99 * Locate a strategy by name.
101 * @param name
102 * name of the strategy to locate.
103 * @return the strategy instance; null if no strategy matches the name.
105 public static synchronized MergeStrategy get(final String name) {
106 return STRATEGIES.get(name);
110 * Get all registered strategies.
112 * @return the registered strategy instances. No inherit order is returned;
113 * the caller may modify (and/or sort) the returned array if
114 * necessary to obtain a reasonable ordering.
116 public static synchronized MergeStrategy[] get() {
117 final MergeStrategy[] r = new MergeStrategy[STRATEGIES.size()];
118 STRATEGIES.values().toArray(r);
119 return r;
122 /** @return default name of this strategy implementation. */
123 public abstract String getName();
126 * Create a new merge instance.
128 * @param db
129 * repository database the merger will read from, and eventually
130 * write results back to.
131 * @return the new merge instance which implements this strategy.
133 public abstract Merger newMerger(Repository db);