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 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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 static org.eclipse.jgit.util.StringUtils.toLowerCase;
  54. import java.util.AbstractSet;
  55. import java.util.ArrayList;
  56. import java.util.Collections;
  57. import java.util.Comparator;
  58. import java.util.HashMap;
  59. import java.util.Iterator;
  60. import java.util.LinkedHashMap;
  61. import java.util.LinkedHashSet;
  62. import java.util.List;
  63. import java.util.Map;
  64. import java.util.Set;
  65. import java.util.concurrent.ConcurrentHashMap;
  66. import org.eclipse.jgit.util.StringUtils;
  67. class ConfigSnapshot {
  68. final List<ConfigLine> entryList;
  69. final Map<Object, Object> cache;
  70. final ConfigSnapshot baseState;
  71. volatile List<ConfigLine> sorted;
  72. volatile SectionNames names;
  73. ConfigSnapshot(List<ConfigLine> entries, ConfigSnapshot base) {
  74. entryList = entries;
  75. cache = new ConcurrentHashMap<Object, Object>(16, 0.75f, 1);
  76. baseState = base;
  77. }
  78. Set<String> getSections() {
  79. return names().sections;
  80. }
  81. Set<String> getSubsections(String section) {
  82. Map<String, Set<String>> m = names().subsections;
  83. Set<String> r = m.get(section);
  84. if (r == null)
  85. r = m.get(toLowerCase(section));
  86. if (r == null)
  87. return Collections.emptySet();
  88. return Collections.unmodifiableSet(r);
  89. }
  90. Set<String> getNames(String section, String subsection) {
  91. List<ConfigLine> s = sorted();
  92. int idx = find(s, section, subsection, "");
  93. if (idx < 0)
  94. idx = -(idx + 1);
  95. Map<String, String> m = new LinkedHashMap<String, String>();
  96. while (idx < s.size()) {
  97. ConfigLine e = s.get(idx++);
  98. if (!e.match(section, subsection))
  99. break;
  100. if (e.name == null)
  101. continue;
  102. String l = toLowerCase(e.name);
  103. if (!m.containsKey(l))
  104. m.put(l, e.name);
  105. }
  106. return new CaseFoldingSet(m);
  107. }
  108. String[] get(String section, String subsection, String name) {
  109. List<ConfigLine> s = sorted();
  110. int idx = find(s, section, subsection, name);
  111. if (idx < 0)
  112. return null;
  113. int end = end(s, idx, section, subsection, name);
  114. String[] r = new String[end - idx];
  115. for (int i = 0; idx < end;)
  116. r[i++] = s.get(idx++).value;
  117. return r;
  118. }
  119. private int find(List<ConfigLine> s, String s1, String s2, String name) {
  120. int low = 0;
  121. int high = s.size();
  122. while (low < high) {
  123. int mid = (low + high) >>> 1;
  124. ConfigLine e = s.get(mid);
  125. int cmp = compare2(
  126. s1, s2, name,
  127. e.section, e.subsection, e.name);
  128. if (cmp < 0)
  129. high = mid;
  130. else if (cmp == 0)
  131. return first(s, mid, s1, s2, name);
  132. else
  133. low = mid + 1;
  134. }
  135. return -(low + 1);
  136. }
  137. private int first(List<ConfigLine> s, int i, String s1, String s2, String n) {
  138. while (0 < i) {
  139. if (s.get(i - 1).match(s1, s2, n))
  140. i--;
  141. else
  142. return i;
  143. }
  144. return i;
  145. }
  146. private int end(List<ConfigLine> s, int i, String s1, String s2, String n) {
  147. while (i < s.size()) {
  148. if (s.get(i).match(s1, s2, n))
  149. i++;
  150. else
  151. return i;
  152. }
  153. return i;
  154. }
  155. private List<ConfigLine> sorted() {
  156. List<ConfigLine> r = sorted;
  157. if (r == null)
  158. sorted = r = sort(entryList);
  159. return r;
  160. }
  161. private static List<ConfigLine> sort(List<ConfigLine> in) {
  162. List<ConfigLine> sorted = new ArrayList<ConfigLine>(in.size());
  163. for (ConfigLine line : in) {
  164. if (line.section != null && line.name != null)
  165. sorted.add(line);
  166. }
  167. Collections.sort(sorted, new LineComparator());
  168. return sorted;
  169. }
  170. private static int compare2(
  171. String aSection, String aSubsection, String aName,
  172. String bSection, String bSubsection, String bName) {
  173. int c = compareIgnoreCase(aSection, bSection);
  174. if (c != 0)
  175. return c;
  176. if (aSubsection == null && bSubsection != null)
  177. return -1;
  178. if (aSubsection != null && bSubsection == null)
  179. return 1;
  180. if (aSubsection != null) {
  181. c = compareWithCase(aSubsection, bSubsection);
  182. if (c != 0)
  183. return c;
  184. }
  185. return compareIgnoreCase(aName, bName);
  186. }
  187. private static class LineComparator implements Comparator<ConfigLine> {
  188. public int compare(ConfigLine a, ConfigLine b) {
  189. return compare2(
  190. a.section, a.subsection, a.name,
  191. b.section, b.subsection, b.name);
  192. }
  193. }
  194. private SectionNames names() {
  195. SectionNames n = names;
  196. if (n == null)
  197. names = n = new SectionNames(this);
  198. return n;
  199. }
  200. private static class SectionNames {
  201. final CaseFoldingSet sections;
  202. final Map<String, Set<String>> subsections;
  203. SectionNames(ConfigSnapshot cfg) {
  204. Map<String, String> sec = new LinkedHashMap<String, String>();
  205. Map<String, Set<String>> sub = new HashMap<String, Set<String>>();
  206. while (cfg != null) {
  207. for (ConfigLine e : cfg.entryList) {
  208. if (e.section == null)
  209. continue;
  210. String l1 = toLowerCase(e.section);
  211. if (!sec.containsKey(l1))
  212. sec.put(l1, e.section);
  213. if (e.subsection == null)
  214. continue;
  215. Set<String> m = sub.get(l1);
  216. if (m == null) {
  217. m = new LinkedHashSet<String>();
  218. sub.put(l1, m);
  219. }
  220. m.add(e.subsection);
  221. }
  222. cfg = cfg.baseState;
  223. }
  224. sections = new CaseFoldingSet(sec);
  225. subsections = sub;
  226. }
  227. }
  228. private static class CaseFoldingSet extends AbstractSet<String> {
  229. private final Map<String, String> names;
  230. CaseFoldingSet(Map<String, String> names) {
  231. this.names = names;
  232. }
  233. @Override
  234. public boolean contains(Object needle) {
  235. if (needle instanceof String) {
  236. String n = (String) needle;
  237. return names.containsKey(n)
  238. || names.containsKey(StringUtils.toLowerCase(n));
  239. }
  240. return false;
  241. }
  242. @Override
  243. public Iterator<String> iterator() {
  244. final Iterator<String> i = names.values().iterator();
  245. return new Iterator<String>() {
  246. public boolean hasNext() {
  247. return i.hasNext();
  248. }
  249. public String next() {
  250. return i.next();
  251. }
  252. public void remove() {
  253. throw new UnsupportedOperationException();
  254. }
  255. };
  256. }
  257. @Override
  258. public int size() {
  259. return names.size();
  260. }
  261. }
  262. }