| /** |
| * Copyright (c) 2011, 2015 - Lunifera GmbH (Gross Enzersdorf, Austria), Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Florian Pirchner - Initial implementation |
| */ |
| package org.eclipse.osbp.runtime.common.state.impl; |
| |
| import java.util.HashMap; |
| import java.util.Map; |
| |
| import org.eclipse.osbp.runtime.common.annotations.DtoUtils; |
| import org.eclipse.osbp.runtime.common.dispose.AbstractDisposable; |
| import org.eclipse.osbp.runtime.common.dispose.IReferenceCountable; |
| import org.eclipse.osbp.runtime.common.hash.HashUtil; |
| import org.eclipse.osbp.runtime.common.state.IDataState; |
| import org.eclipse.osbp.runtime.common.state.ISharedStateContext; |
| |
| // TODO: Auto-generated Javadoc |
| /** |
| * The Class SharedStateContext. |
| */ |
| public class SharedStateContext extends AbstractDisposable implements |
| ISharedStateContext, IReferenceCountable { |
| |
| /** The data. */ |
| private Map<Object, Object> data; |
| |
| /** The id. */ |
| private final String id; |
| |
| /** The dirty state. */ |
| private DirtyDataState dirtyState; |
| |
| /** The global state. */ |
| private DataState globalState; |
| |
| /** The usage count. */ |
| private int usageCount; |
| |
| /** |
| * Instantiates a new shared state context. |
| * |
| * @param id |
| * the id |
| */ |
| public SharedStateContext(String id) { |
| this.id = id; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.common.state.ISharedStateContext#getId() |
| */ |
| @Override |
| public String getId() { |
| return id; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.common.state.ISharedStateContext#setProperty(java.lang.Object, java.lang.Object) |
| */ |
| @Override |
| public void setProperty(Object key, Object value) { |
| checkDisposed(); |
| |
| if (data == null) { |
| data = new HashMap<Object, Object>(); |
| } |
| data.put(key, value); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.common.state.ISharedStateContext#getProperty(java.lang.Object) |
| */ |
| @Override |
| public Object getProperty(Object key) { |
| checkDisposed(); |
| |
| if (data == null) { |
| return null; |
| } |
| return data.get(key); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.common.state.ISharedStateContext#getPropertyKeys() |
| */ |
| @Override |
| public Object[] getPropertyKeys() { |
| checkDisposed(); |
| |
| if (data == null) { |
| return new Object[0]; |
| } |
| return data.keySet().toArray(); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.common.state.ISharedStateContext#getDirtyState() |
| */ |
| @Override |
| public IDataState getDirtyState() { |
| checkDisposed(); |
| |
| if (dirtyState == null) { |
| dirtyState = new DirtyDataState(this); |
| } |
| return dirtyState; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.common.state.ISharedStateContext#getGlobalDataState() |
| */ |
| @Override |
| public IDataState getGlobalDataState() { |
| checkDisposed(); |
| |
| if (globalState == null) { |
| globalState = new GlobalDataState(this); |
| } |
| return globalState; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.common.state.ISharedStateContext#getDirtyAwareGlobalState() |
| */ |
| @Override |
| public IDataState getDirtyAwareGlobalState() { |
| return new DirtyDataAwareGlobalState(getGlobalDataState(), |
| getDirtyState()); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.common.dispose.AbstractDisposable#internalDispose() |
| */ |
| @Override |
| protected void internalDispose() { |
| if (data != null) { |
| data.clear(); |
| data = null; |
| } |
| |
| if (dirtyState != null) { |
| dirtyState.dispose(); |
| dirtyState = null; |
| } |
| |
| if (globalState != null) { |
| globalState.dispose(); |
| globalState = null; |
| } |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.common.dispose.IReferenceCountable#getUsageCount() |
| */ |
| @Override |
| public int getUsageCount() { |
| return usageCount; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.common.dispose.IReferenceCountable#increaseUsageCount() |
| */ |
| @Override |
| public void increaseUsageCount() { |
| usageCount++; |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.common.dispose.IReferenceCountable#decreaseUsageCount() |
| */ |
| @Override |
| public void decreaseUsageCount() { |
| usageCount--; |
| } |
| |
| /** |
| * Invalidate. |
| * |
| * @param key |
| * the key |
| * @param object |
| * the object |
| */ |
| public void invalidate(String key, Object object) { |
| if (dirtyState != null) { |
| dirtyState.invalidate(key); |
| } |
| if (globalState != null) { |
| globalState.invalidate(key); |
| } |
| } |
| |
| /** |
| * Handle dirty. |
| * |
| * @param key |
| * the key |
| * @param object |
| * the object |
| */ |
| public void handleDirty(String key, Object object) { |
| if (dirtyState != null) { |
| dirtyState.register(key, object); |
| } |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.common.state.ISharedStateContext#makeUndirty(java.lang.Object, java.lang.Object) |
| */ |
| @Override |
| public void makeUndirty(Object key, Object dto) { |
| getDirtyState().invalidate(key); |
| |
| // set the dirty flag if available to false |
| DtoUtils.invokeDirtySetter(dto, false); |
| } |
| |
| /* (non-Javadoc) |
| * @see org.eclipse.osbp.runtime.common.state.ISharedStateContext#addNewTransient(java.lang.Object) |
| */ |
| @Override |
| public void addNewTransient(Object newEntry) { |
| // set the dirty flag if available to false |
| DtoUtils.invokeDirtySetter(newEntry, true); |
| getDirtyState().register( |
| HashUtil.createObjectWithIdHash(newEntry.getClass(), newEntry), |
| newEntry); |
| } |
| } |