summaryrefslogtreecommitdiffstats
path: root/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/info/FileInfo.java
blob: 345a260cee5aa5560bb0d8e2ea36c944c68adc96 (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
// Copyright (C) 2013 The Android Open Source Project
//
// 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.google.gerrit.client.info;

import com.google.gerrit.client.rpc.Natives;
import com.google.gerrit.common.data.FilenameComparator;
import com.google.gerrit.reviewdb.client.Patch;
import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray;
import java.util.Collections;
import java.util.Comparator;

public class FileInfo extends JavaScriptObject {
  public final native String path() /*-{ return this.path; }-*/;

  public final native String oldPath() /*-{ return this.old_path; }-*/;

  public final native int linesInserted() /*-{ return this.lines_inserted || 0; }-*/;

  public final native int linesDeleted() /*-{ return this.lines_deleted || 0; }-*/;

  public final native boolean binary() /*-{ return this.binary || false; }-*/;

  public final native String status() /*-{ return this.status; }-*/;

  // JSNI methods cannot have 'long' as a parameter type or a return type and
  // it's suggested to use double in this case:
  // http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html#important
  public final long size() {
    return (long) _size();
  }

  private native double _size() /*-{ return this.size || 0; }-*/;

  public final long sizeDelta() {
    return (long) _sizeDelta();
  }

  private native double _sizeDelta() /*-{ return this.size_delta || 0; }-*/;

  public final native int _row() /*-{ return this._row }-*/;

  public final native void _row(int r) /*-{ this._row = r }-*/;

  public static void sortFileInfoByPath(JsArray<FileInfo> list) {
    Collections.sort(
        Natives.asList(list), Comparator.comparing(FileInfo::path, FilenameComparator.INSTANCE));
  }

  public static String getFileName(String path) {
    String fileName;
    if (Patch.COMMIT_MSG.equals(path)) {
      fileName = "Commit Message";
    } else if (Patch.MERGE_LIST.equals(path)) {
      fileName = "Merge List";
    } else {
      fileName = path;
    }

    int s = fileName.lastIndexOf('/');
    return s >= 0 ? fileName.substring(s + 1) : fileName;
  }

  protected FileInfo() {}
}