<commit_before>package com.gitblit;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.function.Function;

/**
 * Simple class with the only purpose to save the realm file (users.conf) in
 * a fast efficient manner. The JGit Config classes used previously caused
 * a massive CPU hog if the users file got bigger than about 30000 lines.
 *
 * @author <NAME>
 *
 */
public class StoredUserConfig {

	private final File realmFileCopy;
	private SortedMap<String, Section> sections = new TreeMap<>();

	public StoredUserConfig(File realmFileCopy) {
		this.realmFileCopy = realmFileCopy;
	}

	public void setString(final String section, final String subsection, String name, String value) {
		Section s = sections.computeIfAbsent(generateKey(section, subsection), new Function<String, Section>() {
			@Override
			public Section apply(String k) {
				return new Section(section, subsection);
			}
		});
		s.addEntry(name, value);
	}

	public void setBoolean(String section, String subsection, String name, boolean value) {
		setString(section, subsection, name, String.valueOf(value));
	}

	public void setStringList(String section, String subsection, String name, List<String> list) {
		for (String value : list) {
			setString(section, subsection, name, value);
		}
	}

	public void save() throws IOException {
		try (FileWriter fileWriter = new FileWriter(realmFileCopy);
				PrintWriter printWriter = new PrintWriter(fileWriter);) {
			for (Map.Entry<String,Section> entry : sections.entrySet()) {<commit_msg>Fix: Make CPU hog fix Java 7 compatible<commit_after>/*
 * Copyright 2021 gitblit.com, <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gitblit;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.SortedMap;
import java.util.TreeMap;

/**
 * Simple class with the only purpose to save the realm file (users.conf) in
 * a fast efficient manner. The JGit Config classes used previously caused
 * a massive CPU hog if the users file got bigger than about 30000 lines.
 *
 * @author <NAME>
 *
 */
public class StoredUserConfig {

	private final File realmFileCopy;
	private SortedMap<String, Section> sections = new TreeMap<>();

	public StoredUserConfig(File realmFileCopy) {
		this.realmFileCopy = realmFileCopy;
	}

	public void setString(final String section, final String subsection, String name, String value) {
		String key = generateKey(section, subsection);
		Section s = sections.get(key);
		if (s == null) {
			s = new Section(section, subsection);
			sections.put(key, s);
		}
		s.addEntry(name, value);
	}

	public void setBoolean(String section, String subsection, String name, boolean value) {
		setString(section, subsection, name, String.valueOf(value));
	}

	public void setStringList(String section, String subsection, String name, List<String> list) {
		for (String value : list) {
			setString(section, subsection, name, value);
		}
	}

	public void save() throws IOException {
		try (FileWriter fileWriter = new FileWriter(realmFileCopy);
				PrintWriter printWriter = new PrintWriter(fileWriter);) {
			for (Map.Entry<String,Section> entry : sections.entrySet()) {
{'fraction_non_alphanumeric': 0.09719767735420348, 'fraction_numerical': 0.004796768492804847, 'mean_word_length': 4.556802244039271, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '818013', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1114, 'n_words': 408}