summaryrefslogtreecommitdiff
path: root/plugins/cvs/javacvs-src/org/netbeans/lib/cvsclient/admin/Entries.java
blob: f23542f30593b17657daaa1df7a0e8122b113f79 (plain)
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */
package org.netbeans.lib.cvsclient.admin;

import com.intellij.openapi.util.io.FileUtil;
import org.netbeans.lib.cvsclient.util.BugLog;
import org.netbeans.lib.cvsclient.JavaCvsSrcBundle;
import org.jetbrains.annotations.NonNls;

import java.io.*;
import java.util.*;

/**
 * @author Thomas Singer
 */
public final class Entries {

  // Fields =================================================================

  private final Map<String, Entry> fileNameToEntryMap = new HashMap<String, Entry>();
  @NonNls private static final String DIRECTORY_PREFIX = "D";

  // Accessing ==============================================================

  public void addEntry(Entry entry) {
    BugLog.getInstance().assertNotNull(entry);
    BugLog.getInstance().assertNotNull(entry.getFileName());

    fileNameToEntryMap.put(entry.getFileName(), entry);
  }

  public boolean removeEntry(String fileName) {
    BugLog.getInstance().assertNotNull(fileName);

    return fileNameToEntryMap.remove(fileName) != null;
  }

  public Entry getEntry(String fileName) {
    return fileNameToEntryMap.get(fileName);
  }

  public Collection<Entry> getEntries() {
    return Collections.unmodifiableCollection(fileNameToEntryMap.values());
  }

  public void getEntries(Collection<Entry> collection) {
    collection.addAll(fileNameToEntryMap.values());
  }

  public int size() {
    return fileNameToEntryMap.size();
  }

  // Actions ================================================================

  public void read(File entriesFile, final String charsetName) throws IOException {
    try {
      read(new InputStreamReader(new FileInputStream(entriesFile), charsetName));
    }
    catch (InvalidEntryFormatException ex) {
      ex.setEntriesFile(entriesFile);
      throw ex;
    }
  }

  public void read(Reader reader) throws IOException {
    final BufferedReader lineReader = new BufferedReader(reader);
    try {
      for (String line = lineReader.readLine(); line != null; line = lineReader.readLine()) {
        if (line.trim().length() == 0) {
          continue;
        }

        if (line.startsWith(DIRECTORY_PREFIX) && line.trim().length() == 1) {
          continue;
        }

        addEntry(Entry.createEntryForLine(line));
      }
    }
    finally {
      lineReader.close();
    }
  }

  public void write(File entriesFile, String lineSeparator, final String charsetName) throws IOException {
    final File tempFile = new File(entriesFile.getAbsolutePath() + "~");
    FileOutputStream fileOutputStream = new FileOutputStream(tempFile);
    final OutputStreamWriter writer = new OutputStreamWriter(fileOutputStream, charsetName);
    write(writer, lineSeparator);
    if (entriesFile.exists()) {
      if (!entriesFile.delete()) {
        throw new IOException(JavaCvsSrcBundle.message("could.not.delete.file.error.message", entriesFile.getAbsolutePath()));
      }
    }

    FileUtil.rename(tempFile, entriesFile);
  }

  public void write(Writer writer, String lineSeparator) throws IOException {
    writer = new BufferedWriter(writer);
    try {
      if (fileNameToEntryMap.size() == 0) {
        writer.write(DIRECTORY_PREFIX);
        writer.write(lineSeparator);
      }
      else {
        final Entry[] entryArray = new Entry[fileNameToEntryMap.size()];
        fileNameToEntryMap.values().toArray(entryArray);
        Arrays.sort(entryArray, new EntriesComparator());
        for (final Entry entry : entryArray) {
          writer.write(entry.toString());
          writer.write(lineSeparator);
        }
      }
    }
    finally {
      writer.close();
    }
  }

  // Inner classes ==========================================================

  private static final class EntriesComparator implements Comparator<Entry> {

    public int compare(Entry entry1, Entry entry2) {
      if (entry1.isDirectory() != entry2.isDirectory()) {
        if (entry1.isDirectory()) {
          return -1;
        }
        return +1;
      }
      return entry1.getFileName().compareTo(entry2.getFileName());
    }
  }
}