StagingView wrongly sorted by state initially
[egit/eclipse.git] / org.eclipse.egit.ui / src / org / eclipse / egit / ui / internal / KnownHosts.java
blob86e2e175cdae6bdb701e5f3093ce236e24b9e12b
1 /*******************************************************************************
2 * Copyright (C) 2016, Thomas Wolf <thomas.wolf@paranor.ch>
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License 2.0
6 * which accompanies this distribution, and is available at
7 * https://www.eclipse.org/legal/epl-2.0/
9 * SPDX-License-Identifier: EPL-2.0
10 *******************************************************************************/
11 package org.eclipse.egit.ui.internal;
13 import java.util.LinkedHashMap;
15 import org.eclipse.egit.ui.Activator;
16 import org.eclipse.jface.dialogs.IDialogSettings;
17 import org.eclipse.jgit.annotations.NonNull;
19 /**
20 * Internal cache of known host names read from the plugin's
21 * {@link IDialogSettings}. To be accessed only in the UI thread. The plugin
22 * should store back the values in its
23 * {@link Activator#stop(org.osgi.framework.BundleContext)} method by calling
24 * {@link #store()}.
26 public final class KnownHosts {
28 private static final String KNOWN_HOSTS_KEY = "EGit.KnownHosts"; //$NON-NLS-1$
30 private static final String[] DEFAULT_HOSTS = { "git.eclipse.org", //$NON-NLS-1$
31 "github.com", "bitbucket.org" }; //$NON-NLS-1$ //$NON-NLS-2$
33 private static HostStore knownHosts;
35 private static boolean modified;
37 private KnownHosts() {
38 // Utility class shall not be instatiated.
41 /**
42 * Determines whether the given host name is known already.
44 * @param hostName
45 * to check
46 * @return {@code true} is the host name is known, {@code false} otherwise.
48 public static boolean isKnownHost(String hostName) {
49 return hostName != null && getKnownHosts().containsKey(hostName);
52 /**
53 * Adds a new host name to the cache on known hosts.
55 * @param hostName
56 * to add
58 public static void addKnownHost(@NonNull String hostName) {
59 getKnownHosts().put(hostName, null);
60 modified = true; // At least the access order has changed
63 /**
64 * Stores back the known host names into the plugin's
65 * {@link IDialogSettings} if they were changed.
67 public static void store() {
68 if (modified) {
69 String[] values = new String[knownHosts.size()];
70 Activator.getDefault().getDialogSettings().put(KNOWN_HOSTS_KEY,
71 knownHosts.keySet().toArray(values));
72 modified = false;
76 private static HostStore getKnownHosts() {
77 if (knownHosts == null) {
78 IDialogSettings settings = Activator.getDefault()
79 .getDialogSettings();
80 String[] values = settings.getArray(KNOWN_HOSTS_KEY);
81 if (values == null) {
82 settings.put(KNOWN_HOSTS_KEY, DEFAULT_HOSTS);
83 values = DEFAULT_HOSTS;
85 knownHosts = new HostStore(values.length);
86 for (int i = values.length - 1; i >= 0; i--) {
87 String host = values[i];
88 if (host != null && !host.isEmpty()) {
89 knownHosts.put(host, null);
93 return knownHosts;
96 @SuppressWarnings("serial")
97 private static class HostStore extends LinkedHashMap<String, String> {
99 private static final int MAXIMUM_SIZE = 200;
101 public HostStore(int size) {
102 super(size < 10 ? 10 : size, 0.75f, true);
105 @Override
106 protected boolean removeEldestEntry(
107 java.util.Map.Entry<String, String> eldest) {
108 return size() > MAXIMUM_SIZE;