2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com
.intellij
.packaging
.impl
.artifacts
;
18 import com
.intellij
.packaging
.elements
.ComplexPackagingElement
;
19 import com
.intellij
.packaging
.elements
.CompositePackagingElement
;
20 import com
.intellij
.packaging
.elements
.PackagingElement
;
21 import com
.intellij
.util
.SmartList
;
22 import com
.intellij
.util
.StringBuilderSpinAllocator
;
23 import org
.jetbrains
.annotations
.NotNull
;
24 import org
.jetbrains
.annotations
.Nullable
;
26 import java
.util
.List
;
31 public class PackagingElementPath
{
32 public static final PackagingElementPath EMPTY
= new PackagingElementPath(null, null);
33 private final PackagingElementPath myParentPath
;
34 private final PackagingElement
<?
> myLastElement
;
36 private PackagingElementPath(PackagingElementPath parentPath
, PackagingElement
<?
> lastElement
) {
37 myParentPath
= parentPath
;
38 myLastElement
= lastElement
;
41 public PackagingElementPath
appendComplex(ComplexPackagingElement
<?
> element
) {
42 return new PackagingElementPath(this, element
);
45 public PackagingElementPath
appendComposite(CompositePackagingElement
<?
> element
) {
46 return new PackagingElementPath(this, element
);
50 public PackagingElementPath
getParentPath() {
54 public PackagingElement
<?
> getLastElement() {
59 public String
getPathString() {
60 return getPathString("/");
64 public String
getPathString(String separator
) {
65 final StringBuilder builder
= StringBuilderSpinAllocator
.alloc();
67 final List
<CompositePackagingElement
<?
>> parents
= getParents();
68 for (int i
= parents
.size() - 1; i
>= 0; i
--) {
69 builder
.append(parents
.get(i
).getName());
71 builder
.append(separator
);
74 return builder
.toString();
77 StringBuilderSpinAllocator
.dispose(builder
);
81 public List
<CompositePackagingElement
<?
>> getParents() {
82 List
<CompositePackagingElement
<?
>> result
= new SmartList
<CompositePackagingElement
<?
>>();
83 PackagingElementPath path
= this;
84 while (path
!= EMPTY
) {
85 if (path
.myLastElement
instanceof CompositePackagingElement
<?
>) {
86 result
.add((CompositePackagingElement
)path
.myLastElement
);
88 path
= path
.myParentPath
;
93 public List
<PackagingElement
<?
>> getAllElements() {
94 List
<PackagingElement
<?
>> result
= new SmartList
<PackagingElement
<?
>>();
95 PackagingElementPath path
= this;
96 while (path
!= EMPTY
) {
97 result
.add(path
.myLastElement
);
98 path
= path
.myParentPath
;
104 public CompositePackagingElement
<?
> getLastParent() {
105 PackagingElementPath path
= this;
106 while (path
!= EMPTY
) {
107 if (path
.myLastElement
instanceof CompositePackagingElement
<?
>) {
108 return (CompositePackagingElement
)path
.myLastElement
;
114 public boolean isEmpty() {
115 return myParentPath
== null;