
org.glassfish.pfl.dynamic.codegen.impl.AttributedObjectBase Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of pfl-dynamic Show documentation
Show all versions of pfl-dynamic Show documentation
Functionality that may include class generation
/*
* Copyright (c) 1997, 2018 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Distribution License v. 1.0, which is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
package org.glassfish.pfl.dynamic.codegen.impl;
import java.util.List ;
import java.util.ArrayList ;
import java.lang.Math ;
import org.glassfish.pfl.dynamic.copyobject.spi.Copy ;
import org.glassfish.pfl.dynamic.copyobject.spi.CopyInterceptor ;
import org.glassfish.pfl.dynamic.copyobject.spi.CopyType ;
/** An implementation of the AttributedObject interface. This implementation
* supports lazy copying of AttributedObject instances.
*/
public class AttributedObjectBase implements AttributedObject, CopyInterceptor {
// Copying AttributedObjectBase is complicated, because we want to make
// sure that after the copy, source and dest have the same attributes,
// but whenever an attribute is set either in the source or the dest, it
// is not visible to the other node. We do this by making both the
// source and the dest point to the same delegate, which carries the
// attributes originally present in the source. This happens in 3 parts:
// 1. The preCopy method is invoked, which introduces a new delegate in the
// source object, which carries the sources old delegate and attributes.
// 2. During the copy, the destination object's delegate points to the source object.
// 3. After the copy, the destination object's delegate is moved to the source
// object's delegate in the postCopy method.
// At this point, the source and dest share the same delegate, which contains the
// original attributes of the source object. Any set calls on either the
// source or dest will allocate a new attributes objects, which will be used for
// all attribute setting.
// This method is invoked on the source before the copy. If the
// source has attributes, this causes the source to delegate to a new
// AttributedObjectBase object which has the attributes that the source
// originally had. The source ends up having a delegate but no attributes.
// If it also had a delegate originally, the new delegate delegates to the old
// delegate.
public void preCopy() {
if (attributes != null) {
AttributedObjectBase base = new AttributedObjectBase() ;
base.delegate = this.delegate ;
this.delegate = base ;
base.attributes = this.attributes ;
this.attributes = null ;
}
}
// This method is invoked on the result of the copy after all fields have been
// copied. If the result has a delegate, the delegate points to the source,
// but we really want to point to the source's delegate, thus avoiding any conflict
// between the source and the result setting attribute values.
public void postCopy() {
if (delegate != null)
delegate = delegate.delegate ;
}
@Copy(CopyType.SOURCE) // delegate is set to the source object in the copy
private AttributedObjectBase delegate = null ;
@Copy(CopyType.NULL) // attributes is set to null in the copy.
private ArrayList
© 2015 - 2025 Weber Informatics LLC | Privacy Policy