Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * #%L
   * Wikitty :: api
   * %%
   * Copyright (C) 2009 - 2010 CodeLutin, Benjamin Poussin
   * %%
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as 
   * published by the Free Software Foundation, either version 3 of the 
  * License, or (at your option) any later version.
  * 
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Lesser Public License for more details.
  * 
  * You should have received a copy of the GNU General Lesser Public 
  * License along with this program.  If not, see
  * <http://www.gnu.org/licenses/lgpl-3.0.html>.
  * #L%
  */
 package org.nuiton.wikitty.entities;
 
 import java.util.Date;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
this class wrap a wikitty in a proxy that copy the wikitty before applying any change It is used for caching purpose. This class own a reference to an actual wikitty, multiple instance of this class can own some references to a same actual wikitty. To prevent the target to be modified, a copy is created to prevent side-effect. So, when a wikitty restored from cache is modified, a copy is modified, so if change are cancelled, next restore will restore the original and not the modified version (until the modified version is stored). used in WikittyServiceCached
 
 public class WikittyCopyOnWrite implements Wikitty {
 
     private static final long serialVersionUID = 1L;
 
     private static final Log log = LogFactory.getLog(WikittyCopyOnWrite.class);
 
     protected Wikitty target;
 
     protected boolean targetIsACopy = false;

    
only WikittyService with cache should create instances
 
     public WikittyCopyOnWrite(Wikitty target) {
         this. = target;
     }
 
     @Override
     public String getPreloadPattern() {
         return .getPreloadPattern();
     }
 
     @Override
     public void setPreloadPattern(String preloadPattern) {
         if (!ObjectUtils.equals(preloadPatterngetPreloadPattern())) {
             substituteTargetWithCopy();
             .setPreloadPattern(preloadPattern);
         }
     }
 
     @Override
     public Set<StringgetAllPreloadPattern() {
         return .getAllPreloadPattern();
     }
 
     @Override
     public Map<StringWikittygetPreloaded() {
         return .getPreloaded();
     }
 
     @Override
     public void setPreloaded(Map<StringWikittypreloaded) {
         substituteTargetWithCopy();
         .setPreloaded(preloaded);
     }
 
     @Override
     public void addPreloaded(Wikitty w) {
         if (w != null) {
             if (!getPreloaded().containsKey(w.getWikittyId())) {
                 substituteTargetWithCopy();
                 .addPreloaded(w);
            }
        }
    }
    @Override
    public void replaceWith(Wikitty w) {
        replaceWith(wfalse);
    }
    @Override
    public void replaceWith(Wikitty wboolean force) {
        if (this == w) {
            return;
        }
        substituteTargetWithCopy();
        .replaceWith(wforce);
    }
    public Wikitty getTarget() {
        return ;
    }

    
replaceWith target with a clone this method must be called to prevent any modification on target
    protected void substituteTargetWithCopy() {
        // test make a the copy once
        if (! ) {
            try {
                 = .clone();
                 = true;
                if (.isTraceEnabled()) {
                    .trace(this + " now has for target " + );
                }
            } catch (CloneNotSupportedException eee) {
                throw new WikittyException(String.format(
                        "unable to clone wikitty %s"), eee);
            }
        }
    }

    
ATTENTION, la condition d'egalite n'est pas faite sur le type d'objet, mais seulement sur le contenu des donnees. Le equals repond a la question "Est-ce qu'on represente le meme wikitty ?".

Parameters:
obj
Returns:
    @Override
    public boolean equals(Object obj) {
        return .equals(obj);
    }
    @Override
    public String toString(String extName) {
        return .toString(extName);
    }
    public String toStringAllField() {
        return .toStringAllField();
    }
    @Override
    public int hashCode() {
        return .hashCode();
    }
    @Override
    public String toString() {
        return .toString();
    }
    @Override
    public Wikitty clone() throws CloneNotSupportedException {
        // return a clone of the target
        return .clone();
    }
    /* ** below are only delegation code with copy-on-write */
    @Override
    public void addPropertyChangeListener(PropertyChangeListener listener) {
        substituteTargetWithCopy();
        .addPropertyChangeListener(listener);
    }
    @Override
    public void removePropertyChangeListener(PropertyChangeListener listener) {
        substituteTargetWithCopy();
        .removePropertyChangeListener(listener);
    }
    @Override
    public void addPropertyChangeListener(String propertyName,
            PropertyChangeListener listener) {
        substituteTargetWithCopy();
        .addPropertyChangeListener(propertyNamelistener);
    }
    @Override
    public void removePropertyChangeListener(String propertyName,
            PropertyChangeListener listener) {
        substituteTargetWithCopy();
        .removePropertyChangeListener(propertyNamelistener);
    }
    @Override
    public String getId() {
        return .getWikittyId();
    }
    public String getWikittyId() {
        return .getWikittyId();
    }
    @Override
    public boolean isDeleted() {
        return .isDeleted();
    }
    @Override
    public Date getDeleteDate() {
        return .getDeleteDate();
    }
    @Override
    public void setDeleteDate(Date delete) {
        if (!ObjectUtils.equals(deletegetDeleteDate())) {
            substituteTargetWithCopy();
            .setDeleteDate(delete);
        }
    }
    @Override
    public void addExtension(WikittyExtension ext) {
        if (!.getExtensions().contains(ext)) {
            substituteTargetWithCopy();
            .addExtension(ext);
        }
    }
    @Override
    public void addExtension(Collection<WikittyExtensionexts) {
        if (!.getExtensions().containsAll(exts)) {
            substituteTargetWithCopy();
            .addExtension(exts);
        }
    }
    @Override
    public void removeExtension(String ext) {
        if (.getExtensionNames().contains(ext)) {
            substituteTargetWithCopy();
            .removeExtension(ext);
        }
    }
    @Override
    public void removeExtensions(Collection<Stringext) {
        if (CollectionUtils.containsAny(.getExtensionNames(), ext)) {
            substituteTargetWithCopy();
            .removeExtensions(ext);
        }
    }
    @Override
    public boolean hasExtension(String extName) {
        return .hasExtension(extName);
    }
    @Override
    public boolean hasField(String extNameString fieldName) {
        return .hasField(extNamefieldName);
    }
    @Override
    public boolean hasField(String fqfieldName) {
        return .hasField(fqfieldName);
    }
    @Override
    public WikittyExtension getExtension(String ext) {
        return .getExtension(ext);
    }
    @Override
    public Collection<StringgetExtensionNames() {
        return .getExtensionNames();
    }
    @Override
    public Collection<StringgetExtensionFields(String ext) {
        Collection<Stringresult = .getExtension(ext).getFieldNames();
        return result;
    }
    @Override
        return .getExtensions();
    }
    @Override
            boolean recursively) {
        return .getExtensionDependencies(extrecursively);
    }
    @Override
    public FieldType getFieldType(String fqfieldName) {
        return .getFieldType(fqfieldName);
    }
    @Override
    public FieldType getFieldType(String extNameString fieldName) {
        return .getFieldType(extNamefieldName);
    }
    @Override
    public void setField(String extString fieldNameObject value) {
        substituteTargetWithCopy();
        .setField(extfieldNamevalue);
    }
    @Override
    public Object getFieldAsObject(String extString fieldName) {
        return .getFieldAsObject(extfieldName);
    }
    @Override
    public byte[] getFieldAsBytes(String extString fieldName) {
        return .getFieldAsBytes(extfieldName);
    }
    @Override
    public boolean getFieldAsBoolean(String extString fieldName) {
        return .getFieldAsBoolean(extfieldName);
    }
    @Override
    public BigDecimal getFieldAsBigDecimal(String extString fieldName) {
        return .getFieldAsBigDecimal(extfieldName);
    }
    @Override
    public int getFieldAsInt(String extString fieldName) {
        return .getFieldAsInt(extfieldName);
    }
    @Override
    public long getFieldAsLong(String extString fieldName) {
        return .getFieldAsLong(extfieldName);
    }
    @Override
    public float getFieldAsFloat(String extString fieldName) {
        return .getFieldAsFloat(extfieldName);
    }
    @Override
    public double getFieldAsDouble(String extString fieldName) {
        return .getFieldAsDouble(extfieldName);
    }
    @Override
    public String getFieldAsString(String extString fieldName) {
        return .getFieldAsString(extfieldName);
    }
    @Override
    public Date getFieldAsDate(String extString fieldName) {
        return .getFieldAsDate(extfieldName);
    }
    @Override
    public String getFieldAsWikitty(String extString fieldName) {
        return .getFieldAsWikitty(extfieldName);
    }
    @Override
    public Wikitty getFieldAsWikitty(String extNameString fieldNameboolean exceptionIfNotLoaded) {
        return .getFieldAsWikitty(extNamefieldNameexceptionIfNotLoaded);
    }
    @Override
    public <E> List<E> getFieldAsList(String extString fieldName,
            Class<E> clazz) {
        return .getFieldAsList(extfieldNameclazz);
    }
    @Override
    public List<WikittygetFieldAsWikittyList(String extString fieldName,
    boolean exceptionIfNotLoaded) {
        return .getFieldAsWikittyList(extfieldNameexceptionIfNotLoaded);
    }
    @Override
    public <E> Set<E> getFieldAsSet(String extString fieldNameClass<E> clazz) {
        return .getFieldAsSet(extfieldNameclazz);
    }
    @Override
    public Set<WikittygetFieldAsWikittySet(String extString fieldName,
    boolean exceptionIfNotLoaded) {
        return .getFieldAsWikittySet(extfieldNameexceptionIfNotLoaded);
    }
    @Override
    public void addToField(String extString fieldNameObject value) {
        substituteTargetWithCopy();
        .addToField(extfieldNamevalue);
    }
    @Override
    public void removeFromField(String extString fieldNameObject value) {
        substituteTargetWithCopy();
        .removeFromField(extfieldNamevalue);
    }
    @Override
    public void clearField(String extString fieldName) {
        substituteTargetWithCopy();
        .clearField(extfieldName);
    }

    

See also:
org.nuiton.wikitty.Wikitty#addToField(String , Object )
    @Override
    public void addToField(String fqFieldNameObject value) {
        substituteTargetWithCopy();
        .addToField(fqFieldNamevalue);
    }

    

See also:
org.nuiton.wikitty.Wikitty#removeFromField(String , Object )
    @Override
    public void removeFromField(String fqFieldNameObject value) {
        substituteTargetWithCopy();
        .removeFromField(fqFieldNamevalue);
    }

    

See also:
org.nuiton.wikitty.Wikitty#clearField(String )
    @Override
    public void clearField(String fqFieldName) {
        substituteTargetWithCopy();
        .clearField(fqFieldName);
    }
    @Override
    public Set<StringfieldNames() {
        return .fieldNames();
    }
    @Override
    public Map<StringObjectgetFieldValue() {
        return .getFieldValue();
    }
    @Override
    public Set<StringgetAllFieldNames() {
        return .getAllFieldNames();
    }
    @Override
    public Object getFqField(String fqFieldName) {
        return .getFqField(fqFieldName);
    }
    @Override
    public String getVersion() {
        return .getWikittyVersion();
    }
    @Override
    public void setVersion(String version) {
        substituteTargetWithCopy();
        .setWikittyVersion(version);
    }
    @Override
    public String getWikittyVersion() {
        return .getWikittyVersion();
    }
    @Override
    public void setWikittyVersion(String version) {
        substituteTargetWithCopy();
        .setWikittyVersion(version);
    }

    

See also:
org.nuiton.wikitty.Wikitty#getDirty()
    @Override
    public Set<StringgetDirty() {
        return .getDirty();
    }

    

See also:
org.nuiton.wikitty.Wikitty#clearDirty()
    @Override
    public void clearDirty() {
        substituteTargetWithCopy();
        .clearDirty();
    }

    

See also:
org.nuiton.wikitty.Wikitty#setFqField(String , Object )
    @Override
    public void setFqField(String fieldNameObject value) {
        substituteTargetWithCopy();
        .setFqField(fieldNamevalue);
    }
    @Override
    public boolean isEmpty() {
        return .isEmpty();
    }

    

See also:
org.nuiton.wikitty.Wikitty#hasMetaExtension(String , String )
    @Override
    public boolean hasMetaExtension(String metaExtensionName,
                                    String extensionName) {
        return .hasMetaExtension(metaExtensionNameextensionName);
    }
    
    

See also:
org.nuiton.wikitty.Wikitty#addMetaExtension(WikittyExtension , WikittyExtension )
    @Override
    public void addMetaExtension(WikittyExtension metaExtension,
            WikittyExtension extension) {
        substituteTargetWithCopy();
        .addMetaExtension(metaExtensionextension);
    }

    

See also:
org.nuiton.wikitty.Wikitty#addMetaExtension(WikittyExtension , String )
    @Override
    public void addMetaExtension(WikittyExtension metaExtension,
            String extensionFqn) {
        substituteTargetWithCopy();
        .addMetaExtension(metaExtensionextensionFqn);
    }
    
New to GrepCode? Check out our FAQ X