001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements. See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership. The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License. You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing,
013 * software distributed under the License is distributed on an
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 * KIND, either express or implied. See the License for the
016 * specific language governing permissions and limitations
017 * under the License.
018 */
019package org.apache.commons.weaver.model;
020
021/**
022 * Describes a {@link Weavable} that lives inside some other {@link Weavable}.
023 * @param <SELF> own type
024 * @param <TARGET> weavable target type
025 * @param <PARENT> enclosing weavable type
026 * @param <PARENT_TARGET> parent target type
027 */
028public abstract class NestedWeavable
029    <SELF extends NestedWeavable<SELF, TARGET, PARENT, PARENT_TARGET>,
030    TARGET,
031    PARENT extends Weavable<PARENT, PARENT_TARGET>,
032    PARENT_TARGET>
033    extends Weavable<SELF, TARGET> {
034
035    private final PARENT parent;
036
037    /**
038     * Create a new {@link NestedWeavable} instance.
039     * @param target element
040     * @param parent enclosing
041     */
042    protected NestedWeavable(final TARGET target, final PARENT parent) {
043        super(target);
044        this.parent = parent;
045    }
046
047    /**
048     * Get the parent.
049     * @return {@code PARENT}
050     */
051    public PARENT getParent() {
052        return parent;
053    }
054
055    /**
056     * Implement {@link Comparable}.
057     * @param obj {@code SELF}
058     * @return int per {@link Comparable#compareTo(Object)} contract
059     */
060    @Override
061    public final int compareTo(final SELF obj) {
062        final int result = getParent().compareTo(obj.getParent());
063        return result == 0 ? localCompareTo(obj) : result;
064    }
065
066    /**
067     * Compare against {@code o} without respect to {@link #getParent()}.
068     * @param obj SELF{@code SELF}
069     * @return int per {@link Comparable#compareTo(Object)} contract
070     */
071    protected abstract int localCompareTo(SELF obj);
072}