Line data Source code
1 : // Copyright (C) 2021 The Android Open Source Project 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.entities; 16 : 17 : import java.util.Set; 18 : import org.eclipse.jgit.errors.ConfigInvalidException; 19 : import org.eclipse.jgit.lib.Config; 20 : 21 : /** 22 : * Immutable parsed representation of a {@link org.eclipse.jgit.lib.Config} that can be cached. 23 : * Supports only a limited set of operations. 24 : */ 25 : public class ImmutableConfig { 26 3 : public static final ImmutableConfig EMPTY = new ImmutableConfig("", new Config()); 27 : 28 : private final String stringCfg; 29 : private final Config cfg; 30 : 31 3 : private ImmutableConfig(String stringCfg, Config cfg) { 32 3 : this.stringCfg = stringCfg; 33 3 : this.cfg = cfg; 34 3 : } 35 : 36 : public static ImmutableConfig parse(String stringCfg) throws ConfigInvalidException { 37 3 : Config cfg = new Config(); 38 3 : cfg.fromText(stringCfg); 39 3 : return new ImmutableConfig(stringCfg, cfg); 40 : } 41 : 42 : /** Returns a mutable copy of this config. */ 43 : public Config mutableCopy() { 44 1 : Config cfg = new Config(); 45 : try { 46 1 : cfg.fromText(this.cfg.toText()); 47 0 : } catch (ConfigInvalidException e) { 48 : // Can't happen as we used JGit to format that config. 49 0 : throw new IllegalStateException(e); 50 1 : } 51 1 : return cfg; 52 : } 53 : 54 : /** See {@link Config#getSections()} */ 55 : public Set<String> getSections() { 56 1 : return cfg.getSections(); 57 : } 58 : 59 : /** See {@link Config#getNames(String)} */ 60 : public Set<String> getNames(String section) { 61 1 : return cfg.getNames(section); 62 : } 63 : 64 : /** See {@link Config#getNames(String, String)} */ 65 : public Set<String> getNames(String section, String subsection) { 66 1 : return cfg.getNames(section, subsection); 67 : } 68 : 69 : /** See {@link Config#getStringList(String, String, String)} */ 70 : public String[] getStringList(String section, String subsection, String name) { 71 1 : return cfg.getStringList(section, subsection, name); 72 : } 73 : 74 : /** See {@link Config#getSubsections(String)} */ 75 : public Set<String> getSubsections(String section) { 76 1 : return cfg.getSubsections(section); 77 : } 78 : 79 : @Override 80 : public boolean equals(Object o) { 81 1 : if (!(o instanceof ImmutableConfig)) { 82 0 : return false; 83 : } 84 1 : return ((ImmutableConfig) o).stringCfg.equals(stringCfg); 85 : } 86 : 87 : @Override 88 : public int hashCode() { 89 0 : return stringCfg.hashCode(); 90 : } 91 : }