Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f377ec4c3a23ce77cb5692b2918addcd340d1dff (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
 * Copyright (c) 2014, 2015 Eike Stepper (Loehne, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v2.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v20.html
 *
 * Contributors:
 *    Ed Merks - initial API and implementation
 */
package org.eclipse.oomph.util;

import org.eclipse.oomph.internal.util.UtilPlugin;

import java.io.File;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author Ed Merks
 */
public class PropertyFile
{
  private final File file;

  public PropertyFile(File file)
  {
    this.file = file;
  }

  public String getProperty(String key, String defaultValue)
  {
    Map<String, String> properties = loadProperties();
    String value = properties.get(key);
    return value == null ? defaultValue : value;
  }

  public void setProperty(String key, String value)
  {
    Map<String, String> properties = loadProperties();
    addAndSaveProperty(properties, key, value);
  }

  public boolean compareAndSetProperty(String key, String value, String... expectedValues)
  {
    Map<String, String> properties = loadProperties();
    String existingValue = properties.get(key);

    if (expectedValues.length == 0)
    {
      if (ObjectUtil.equals(existingValue, null))
      {
        addAndSaveProperty(properties, key, value);
        return true;
      }
    }
    else
    {
      for (String expectedValue : expectedValues)
      {
        if (ObjectUtil.equals(existingValue, expectedValue))
        {
          addAndSaveProperty(properties, key, value);
          return true;
        }
      }
    }

    return false;
  }

  public void removeProperty(String key)
  {
    Map<String, String> properties = loadProperties();
    if (properties.remove(key) != null)
    {
      saveProperties(properties);
    }
  }

  public Map<String, String> loadProperties()
  {
    try
    {
      if (file.exists())
      {
        return PropertiesUtil.loadProperties(file);
      }
    }
    catch (RuntimeException ex)
    {
      // Ignore.
    }

    return new LinkedHashMap<String, String>();
  }

  public void saveProperties(Map<String, String> properties)
  {
    try
    {
      PropertiesUtil.saveProperties(file, properties, true);
    }
    catch (RuntimeException ex)
    {
      UtilPlugin.INSTANCE.log(ex);
    }
  }

  private void addAndSaveProperty(Map<String, String> properties, String key, String value)
  {
    properties.put(key, value.toString());
    saveProperties(properties);
  }
}

Back to the top