summaryrefslogtreecommitdiff
path: root/plugins/svn4idea/src/org/jetbrains/idea/svn/history/LogEntry.java
blob: 1b3d12656d2b41465492358b0af7194a4d4c5980 (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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 org.jetbrains.idea.svn.history;

import com.intellij.util.containers.ContainerUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.tmatesoft.svn.core.SVNLogEntry;
import org.tmatesoft.svn.core.SVNLogEntryPath;

import javax.xml.bind.annotation.*;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Konstantin Kolosovsky.
 */
public class LogEntry {

  public static final LogEntry EMPTY = new LogEntry.Builder().setRevision(-1).setHasChildren(false).build();

  private final long myRevision;
  private final Date myDate;
  private final String myMessage;
  private final String myAuthor;
  @NotNull private final Map<String, LogEntryPath> myChangedPaths;
  private boolean myHasChildren;

  @Nullable
  public static LogEntry create(@Nullable SVNLogEntry entry) {
    LogEntry result = null;

    if (entry != null) {
      LogEntry.Builder builder = new LogEntry.Builder();

      if (entry.getChangedPaths() != null) {
        for (SVNLogEntryPath path : entry.getChangedPaths().values()) {
          builder.addPath(LogEntryPath.create(path));
        }
      }

      result = builder.setRevision(entry.getRevision()).setAuthor(entry.getAuthor()).setDate(entry.getDate()).setMessage(entry.getMessage())
        .setHasChildren(entry.hasChildren()).build();
    }

    return result;
  }

  public LogEntry(@NotNull LogEntry.Builder builder) {
    myRevision = builder.revision;
    myChangedPaths = toImmutable(builder.changedPaths);
    myAuthor = builder.author;
    myDate = builder.date;
    myMessage = builder.message;
    myHasChildren = builder.hasChildren();
  }

  @NotNull
  private static Map<String, LogEntryPath> toImmutable(@NotNull List<LogEntryPath.Builder> paths) {
    ContainerUtil.ImmutableMapBuilder<String, LogEntryPath> builder = ContainerUtil.immutableMapBuilder();

    for (LogEntryPath.Builder path : paths) {
      builder.put(path.getPath(), path.build());
    }

    return builder.build();
  }

  @NotNull
  public Map<String, LogEntryPath> getChangedPaths() {
    return myChangedPaths;
  }

  public String getAuthor() {
    return myAuthor;
  }

  public Date getDate() {
    return myDate;
  }

  public String getMessage() {
    return myMessage;
  }

  public long getRevision() {
    return myRevision;
  }

  public boolean hasChildren() {
    return myHasChildren;
  }

  @XmlAccessorType(XmlAccessType.NONE)
  // type explicitly specified not to conflict with LogEntryPath.Builder
  @XmlType(name = "logentry")
  public static class Builder {

    @XmlAttribute(name = "revision")
    private long revision;

    @XmlElement(name = "author")
    private String author;

    @XmlElement(name = "date")
    private Date date;

    @XmlElement(name = "msg")
    private String message;

    @XmlElementWrapper(name = "paths")
    @XmlElement(name = "path")
    private List<LogEntryPath.Builder> changedPaths = ContainerUtil.newArrayList();

    @XmlElement(name = "logentry")
    private List<LogEntry.Builder> childEntries = ContainerUtil.newArrayList();

    @NotNull
    public List<LogEntry.Builder> getChildEntries() {
      return childEntries;
    }

    public boolean hasChildren() {
      return !childEntries.isEmpty();
    }

    @NotNull
    public Builder setRevision(long revision) {
      this.revision = revision;
      return this;
    }

    @NotNull
    public Builder setAuthor(String author) {
      this.author = author;
      return this;
    }

    @NotNull
    public Builder setDate(Date date) {
      this.date = date;
      return this;
    }

    @NotNull
    public Builder setMessage(String message) {
      this.message = message;
      return this;
    }

    @NotNull
    public Builder setHasChildren(boolean hasChildren) {
      // probably LogEntry interface will be changed and child entries will be specified explicitly later, but for now just use such "fake"
      // implementation for setting "hasChildren" value
      childEntries.clear();
      if (hasChildren) {
        childEntries.add(this);
      }
      return this;
    }

    @NotNull
    public Builder addPath(@NotNull LogEntryPath.Builder path) {
      changedPaths.add(path);
      return this;
    }

    @NotNull
    public LogEntry build() {
      return new LogEntry(this);
    }
  }
}