ConfigLine.java

  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-2010, Google Inc.
  6.  * Copyright (C) 2009, Google, Inc.
  7.  * Copyright (C) 2009, JetBrains s.r.o.
  8.  * Copyright (C) 2007-2008, Robin Rosenberg <robin.rosenberg@dewire.com>
  9.  * Copyright (C) 2006-2008, Shawn O. Pearce <spearce@spearce.org>
  10.  * Copyright (C) 2008, Thad Hughes <thadh@thad.corp.google.com> and others
  11.  *
  12.  * This program and the accompanying materials are made available under the
  13.  * terms of the Eclipse Distribution License v. 1.0 which is available at
  14.  * https://www.eclipse.org/org/documents/edl-v10.php.
  15.  *
  16.  * SPDX-License-Identifier: BSD-3-Clause
  17.  */

  18. package org.eclipse.jgit.lib;

  19. import org.eclipse.jgit.util.StringUtils;

  20. /** A line in a Git {@link Config} file. */
  21. class ConfigLine {
  22.     /** The text content before entry. */
  23.     String prefix;

  24.     /** The section name for the entry. */
  25.     String section;

  26.     /** Subsection name. */
  27.     String subsection;

  28.     /** The key name. */
  29.     String name;

  30.     /** The value. */
  31.     String value;

  32.     /** The text content after entry. */
  33.     String suffix;

  34.     /** The source from which this line was included from. */
  35.     String includedFrom;

  36.     ConfigLine forValue(String newValue) {
  37.         final ConfigLine e = new ConfigLine();
  38.         e.prefix = prefix;
  39.         e.section = section;
  40.         e.subsection = subsection;
  41.         e.name = name;
  42.         e.value = newValue;
  43.         e.suffix = suffix;
  44.         e.includedFrom = includedFrom;
  45.         return e;
  46.     }

  47.     boolean match(final String aSection, final String aSubsection,
  48.             final String aKey) {
  49.         return eqIgnoreCase(section, aSection)
  50.                 && eqSameCase(subsection, aSubsection)
  51.                 && eqIgnoreCase(name, aKey);
  52.     }

  53.     boolean match(String aSection, String aSubsection) {
  54.         return eqIgnoreCase(section, aSection)
  55.                 && eqSameCase(subsection, aSubsection);
  56.     }

  57.     private static boolean eqIgnoreCase(String a, String b) {
  58.         if (a == null && b == null)
  59.             return true;
  60.         if (a == null || b == null)
  61.             return false;
  62.         return StringUtils.equalsIgnoreCase(a, b);
  63.     }

  64.     private static boolean eqSameCase(String a, String b) {
  65.         if (a == null && b == null)
  66.             return true;
  67.         if (a == null || b == null)
  68.             return false;
  69.         return a.equals(b);
  70.     }

  71.     /** {@inheritDoc} */
  72.     @SuppressWarnings("nls")
  73.     @Override
  74.     public String toString() {
  75.         if (section == null)
  76.             return "<empty>";
  77.         StringBuilder b = new StringBuilder(section);
  78.         if (subsection != null)
  79.             b.append(".").append(subsection);
  80.         if (name != null)
  81.             b.append(".").append(name);
  82.         if (value != null)
  83.             b.append("=").append(value);
  84.         return b.toString();
  85.     }
  86. }