Skip to content

Instantly share code, notes, and snippets.

@gastaldi
Created October 1, 2013 15:52
Show Gist options
  • Select an option

  • Save gastaldi/6780684 to your computer and use it in GitHub Desktop.

Select an option

Save gastaldi/6780684 to your computer and use it in GitHub Desktop.
/**
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.addon.resource.transaction;
import org.jboss.forge.addon.resource.Resource;
import org.jboss.forge.addon.resource.events.ResourceEvent;
/**
* A {@link Change} object contains references to the original and the modified resource enlisted in a
* {@link ResourceTransaction}
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public interface Change
{
/**
* The description of this change (if any)
*/
String getDescription();
/**
* @return The original {@link Resource}
*/
Resource<?> getOriginalResource();
/**
* @return The modified {@link Resource}
*/
Resource<?> getModifiedResource();
/**
* @return The {@link ResourceEvent} that originated this {@link Change}
*/
ResourceEvent getEvent();
}
/**
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.addon.resource.transaction;
import javax.annotation.Priority;
import javax.decorator.Decorator;
import javax.decorator.Delegate;
import javax.inject.Inject;
import org.jboss.forge.addon.resource.Resource;
import org.jboss.forge.addon.resource.ResourceFactory;
/**
* Adds orthogonal transaction features to the {@link ResourceFactory}
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
@Priority(0)
@Decorator
public abstract class ResourceFactoryTransactionDecorator implements ResourceFactory
{
@Inject
@Delegate
ResourceFactory delegate;
private volatile boolean enabled;
@Override
public <E, T extends Resource<E>> T create(Class<T> type, E underlyingResource)
{
T result = delegate.create(type, underlyingResource);
if (enabled)
{
// Wrap the resource
}
return result;
}
void setEnabled(boolean enabled)
{
this.enabled = enabled;
}
}
/**
* Copyright 2013 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Eclipse Public License version 1.0, available at
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.jboss.forge.addon.resource.transaction;
import java.io.Closeable;
import java.util.Set;
/**
* A {@link ResourceTransaction} manages changes to a group of resources
*
* @author <a href="ggastald@redhat.com">George Gastaldi</a>
*/
public interface ResourceTransaction extends Closeable
{
/**
* Starts a transaction. It does nothing if the transaction was already started. It throws an
* {@link IllegalStateException} if the transaction was closed
*/
public void start();
/**
* Applies every change in the {@link Set} returned by {@link ResourceTransaction#getChangeSet()}
*/
public void commit();
/**
* Discards this transaction and the associated changes with it
*/
public void rollback();
/**
* The changes associated with this transaction.
*
* @return a mutable {@link Set} with the changes that were introduced so far
*/
public Set<Change> getChangeSet();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment