alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 *
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 *
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2003 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.modules.registry.mergedctx;

import org.openide.util.Utilities;

import java.util.Enumeration;
import java.util.StringTokenizer;

/**
 * Encapsulates independent view of hierarchical resource path.
 * Implemented as immutable.
 *
 * @author Radek Matous
 */
final class Resource {
    private final String resourcePath;

    Resource(final String resourcePath) {
        if (resourcePath == null) throw new IllegalArgumentException();
        this.resourcePath = getNormalizedPath(resourcePath);
    }

    boolean isRoot() {
        return (getParent() == null) ? true : false;
    }


    Enumeration getElements() {
        final StringTokenizer sTokens = new StringTokenizer(resourcePath, "/");//NOI18N
        return sTokens;
    }

    boolean isSuperior(final Resource subordered) {
        return subordered.getNormalizedPath().startsWith(getNormalizedPath());
    }

    Resource getChild(final String nameExt) {
        final Resource retVal;
        final StringBuffer sb = new StringBuffer(resourcePath);
        if (!resourcePath.endsWith("/")) sb.append("/");//NOI18N
        sb.append(nameExt);
        retVal = new Resource(sb.toString());
        return retVal;
    }

    Resource getParent() {
        final int idx = resourcePath.lastIndexOf('/');
        if (idx == 0 && resourcePath.length() == 1) return null;
        return new Resource((idx <= 0) ? "/" : resourcePath.substring(0, idx));//NOI18N
    }

    String getName() {
        int idx0 = resourcePath.lastIndexOf('/');//NOI18N
        idx0 = (idx0 == -1) ? 0 : (idx0 + 1);
        return resourcePath.substring(idx0, resourcePath.length());
    }


    String getPath() {
        String retValue = resourcePath;
        //retValue = retValue.replace('/', '/');
        //retValue = retValue.replace('.', '.');
        final int idx = Utilities.isWindows() ? 1 : 0;
        return retValue.substring(idx);
    }

    /** Adds slash at first position if necessary and removes slash from last position */
    private static String getNormalizedPath(String resPath) {
        if (resPath == null) return resPath;
        resPath = resPath.replace('\\', '/');//NOI18N

        if (!resPath.startsWith("/")) //NOI18N
            resPath = "/" + resPath; //NOI18N

        if (resPath.endsWith("/") && resPath.length() != "/".length()) //NOI18N
            resPath = resPath.substring(0, resPath.length() - 1);

        return resPath;
    }

    public int hashCode() {
        return resourcePath.hashCode();
    }

    public boolean equals(final Object obj) {
        String resPath = null;
        if (obj instanceof String) {
            resPath = (String) obj;
        } else if (obj instanceof Resource) {
            resPath = ((Resource) obj).getNormalizedPath();
        }
        return resourcePath.equals(resPath);
    }

    public String toString() {
        return getNormalizedPath();
    }

    private String getNormalizedPath() {
        return resourcePath;
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.