jaulib v1.3.0
Jau Support Library (C++, Java, ..)
DirItem.java
Go to the documentation of this file.
1/**
2 * Author: Sven Gothel <sgothel@jausoft.com>
3 * Copyright (c) 2022 Gothel Software e.K.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24package org.jau.fs;
25
26/**
27 * Representing a directory item split into {@link #dirname()} and {@link #basename()}.
28 */
29public class DirItem {
30 static private final String _slash = "/";
31 static private final String _dot = ".";
32
33 private final String dirname_;
34 private final String basename_;
35
36 /* pp */ DirItem(final String dirname, final String basename) {
37 dirname_ = dirname;
38 basename_ = basename;
39 }
40
41 /** Empty item w/ `.` set for both, dirname and basename */
42 public DirItem() {
43 dirname_ = _dot;
44 basename_ = _dot;
45 }
46
47 /**
48 * Create a dir_item where path is split into dirname and basename after `.` and `..` has been reduced.
49 *
50 * @param path_ the raw path
51 */
52 public DirItem(final String path) {
53 final String[] s2 = getString2DirItem(path);
54 dirname_ = s2[0];
55 basename_ = s2[1];
56 }
57 private static native String[/*3*/] getString2DirItem(final String s);
58
59 /** Returns the dirname, shall not be empty and denotes `.` for current working director. */
60 public String dirname() { return dirname_; }
61
62 /** Return the basename, shall not be empty nor contain a dirname. */
63 public String basename() { return basename_; }
64
65 /**
66 * Returns a full unix path representation combining dirname() and basename().
67 */
68 public String path() {
69 if( _dot.equals( dirname_ ) ) {
70 return basename_;
71 }
72 if( _dot.equals( basename_ ) ) {
73 return dirname_;
74 }
75 if( _slash.equals( dirname_ ) ) {
76 return dirname_ + basename_;
77 }
78 return dirname_ + _slash + basename_;
79 }
80
81 @Override
82 public String toString() {
83 return "['"+dirname()+"', '"+basename()+"']";
84 }
85
86 @Override
87 public boolean equals(final Object other) {
88 if (this == other) {
89 return true;
90 }
91 return (other instanceof DirItem) &&
92 this.dirname_.equals( ((DirItem)other).dirname_ ) &&
93 this.basename_.equals( ((DirItem)other).basename_ );
94 }
95}
Representing a directory item split into dirname() and basename().
Definition: DirItem.java:29
boolean equals(final Object other)
Definition: DirItem.java:87
String basename()
Return the basename, shall not be empty nor contain a dirname.
Definition: DirItem.java:63
DirItem()
Empty item w/ .
Definition: DirItem.java:42
String toString()
Definition: DirItem.java:82
String dirname()
Returns the dirname, shall not be empty and denotes .
Definition: DirItem.java:60
DirItem(final String path)
Create a dir_item where path is split into dirname and basename after .
Definition: DirItem.java:52
String path()
Returns a full unix path representation combining dirname() and basename().
Definition: DirItem.java:68