You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigSnapshot.java 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Copyright (C) 2010, Mathias Kinzler <mathias.kinzler@sap.com>
  3. * Copyright (C) 2009, Constantine Plotnikov <constantine.plotnikov@gmail.com>
  4. * Copyright (C) 2007, Dave Watson <dwatson@mimvista.com>
  5. * Copyright (C) 2008-2012, Google Inc.
  6. * Copyright (C) 2009, JetBrains s.r.o.
  7. * Copyright (C) 2007-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  8. * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  9. * Copyright (C) 2008, Thad Hughes <thadh@thad.corp.google.com>
  10. * and other copyright owners as documented in the project's IP log.
  11. *
  12. * This program and the accompanying materials are made available
  13. * under the terms of the Eclipse Distribution License v1.0 which
  14. * accompanies this distribution, is reproduced below, and is
  15. * available at http://www.eclipse.org/org/documents/edl-v10.php
  16. *
  17. * All rights reserved.
  18. *
  19. * Redistribution and use in source and binary forms, with or
  20. * without modification, are permitted provided that the following
  21. * conditions are met:
  22. *
  23. * - Redistributions of source code must retain the above copyright
  24. * notice, this list of conditions and the following disclaimer.
  25. *
  26. * - Redistributions in binary form must reproduce the above
  27. * copyright notice, this list of conditions and the following
  28. * disclaimer in the documentation and/or other materials provided
  29. * with the distribution.
  30. *
  31. * - Neither the name of the Eclipse Foundation, Inc. nor the
  32. * names of its contributors may be used to endorse or promote
  33. * products derived from this software without specific prior
  34. * written permission.
  35. *
  36. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  37. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  38. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  39. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  40. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  41. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  43. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  44. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  45. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  46. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  47. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  48. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  49. */
  50. package org.eclipse.jgit.lib;
  51. import static org.eclipse.jgit.util.StringUtils.compareIgnoreCase;
  52. import static org.eclipse.jgit.util.StringUtils.compareWithCase;
  53. import java.util.ArrayList;
  54. import java.util.Collections;
  55. import java.util.Comparator;
  56. import java.util.List;
  57. import java.util.Map;
  58. import java.util.concurrent.ConcurrentHashMap;
  59. class ConfigSnapshot {
  60. final List<ConfigLine> entryList;
  61. final Map<Object, Object> cache;
  62. final ConfigSnapshot baseState;
  63. volatile List<ConfigLine> sorted;
  64. ConfigSnapshot(List<ConfigLine> entries, ConfigSnapshot base) {
  65. entryList = entries;
  66. cache = new ConcurrentHashMap<Object, Object>(16, 0.75f, 1);
  67. baseState = base;
  68. }
  69. String[] get(String section, String subsection, String name) {
  70. List<ConfigLine> s = sorted();
  71. int idx = find(s, section, subsection, name);
  72. if (idx < 0)
  73. return null;
  74. int end = end(s, idx, section, subsection, name);
  75. String[] r = new String[end - idx];
  76. for (int i = 0; idx < end;)
  77. r[i++] = s.get(idx++).value;
  78. return r;
  79. }
  80. private int find(List<ConfigLine> s, String s1, String s2, String name) {
  81. int low = 0;
  82. int high = s.size();
  83. while (low < high) {
  84. int mid = (low + high) >>> 1;
  85. ConfigLine e = s.get(mid);
  86. int cmp = compare2(
  87. s1, s2, name,
  88. e.section, e.subsection, e.name);
  89. if (cmp < 0)
  90. high = mid;
  91. else if (cmp == 0)
  92. return first(s, mid, s1, s2, name);
  93. else
  94. low = mid + 1;
  95. }
  96. return -(low + 1);
  97. }
  98. private int first(List<ConfigLine> s, int i, String s1, String s2, String n) {
  99. while (0 < i) {
  100. if (s.get(i - 1).match(s1, s2, n))
  101. i--;
  102. else
  103. return i;
  104. }
  105. return i;
  106. }
  107. private int end(List<ConfigLine> s, int i, String s1, String s2, String n) {
  108. while (i < s.size()) {
  109. if (s.get(i).match(s1, s2, n))
  110. i++;
  111. else
  112. return i;
  113. }
  114. return i;
  115. }
  116. private List<ConfigLine> sorted() {
  117. List<ConfigLine> r = sorted;
  118. if (r == null)
  119. sorted = r = sort(entryList);
  120. return r;
  121. }
  122. private static List<ConfigLine> sort(List<ConfigLine> in) {
  123. List<ConfigLine> sorted = new ArrayList<ConfigLine>(in.size());
  124. for (ConfigLine line : in) {
  125. if (line.section != null && line.name != null)
  126. sorted.add(line);
  127. }
  128. Collections.sort(sorted, new LineComparator());
  129. return sorted;
  130. }
  131. private static int compare2(
  132. String aSection, String aSubsection, String aName,
  133. String bSection, String bSubsection, String bName) {
  134. int c = compareIgnoreCase(aSection, bSection);
  135. if (c != 0)
  136. return c;
  137. if (aSubsection == null && bSubsection != null)
  138. return -1;
  139. if (aSubsection != null && bSubsection == null)
  140. return 1;
  141. if (aSubsection != null) {
  142. c = compareWithCase(aSubsection, bSubsection);
  143. if (c != 0)
  144. return c;
  145. }
  146. return compareIgnoreCase(aName, bName);
  147. }
  148. private static class LineComparator implements Comparator<ConfigLine> {
  149. public int compare(ConfigLine a, ConfigLine b) {
  150. return compare2(
  151. a.section, a.subsection, a.name,
  152. b.section, b.subsection, b.name);
  153. }
  154. }
  155. }