summaryrefslogtreecommitdiff
path: root/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/CustomResourceBundleState.java
blob: 9be149d6a4db6d946756f5f9852c7aef392b3730 (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
/*
 * 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 com.intellij.lang.properties;

import com.intellij.openapi.util.Condition;
import com.intellij.openapi.vfs.VirtualFile;
import com.intellij.openapi.vfs.VirtualFileManager;
import com.intellij.util.Function;
import com.intellij.util.containers.ContainerUtil;
import com.intellij.util.containers.HashSet;
import com.intellij.util.xmlb.annotations.AbstractCollection;
import com.intellij.util.xmlb.annotations.Property;
import com.intellij.util.xmlb.annotations.Tag;
import com.intellij.util.xmlb.annotations.Transient;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Collection;
import java.util.List;
import java.util.Set;

/**
 * @author Dmitry Batkovich
 */
@Tag("custom-resource-bundle")
public class CustomResourceBundleState {

  @Property(surroundWithTag = false)
  @AbstractCollection(surroundWithTag = false, elementTag = "file", elementValueAttribute = "value")
  public Set<String> myFileUrls = new HashSet<String>();

  @Tag("base-name")
  public String myBaseName;

  @Transient
  @NotNull
  public String getBaseName() {
    return myBaseName;
  }

  public Set<String> getFileUrls() {
    return myFileUrls;
  }

  public List<VirtualFile> getFiles(@NotNull final VirtualFileManager manager) {
    return ContainerUtil.mapNotNull(getFileUrls(), new Function<String, VirtualFile>() {
        @Override
        public VirtualFile fun(String url) {
          return manager.findFileByUrl(url);
        }
      });
  }

  @Nullable
  public CustomResourceBundleState removeNonExistentFiles(final VirtualFileManager virtualFileManager) {
    final List<String> existentFiles = ContainerUtil.filter(myFileUrls, new Condition<String>() {
      @Override
      public boolean value(String url) {
        return virtualFileManager.findFileByUrl(url) != null;
      }
    });
    if (existentFiles.isEmpty()) {
      return null;
    }
    final CustomResourceBundleState customResourceBundleState = new CustomResourceBundleState();
    customResourceBundleState.myFileUrls.addAll(existentFiles);
    customResourceBundleState.myBaseName = myBaseName;
    return customResourceBundleState;
  }

  public CustomResourceBundleState addAll(final Collection<String> urls) {
    myFileUrls.addAll(urls);
    return this;
  }

  public CustomResourceBundleState setBaseName(String baseName) {
    myBaseName = baseName;
    return this;
  }
}