![JAR search and dependency download from the Maven repository](/logo.png)
com.ait.tooling.nativetools.client.datamodel.controller.AbstractModelController Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ahome-tooling-nativetools Show documentation
Show all versions of ahome-tooling-nativetools Show documentation
Ahome Tooling NativeTools - GWT high speed native collection, browser access, and JSON utilities.
The newest version!
/*
Copyright (c) 2017 Ahome' Innovation Technologies. All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.ait.tooling.nativetools.client.datamodel.controller;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.function.Consumer;
import java.util.function.Function;
import com.ait.tooling.nativetools.client.NObject;
import com.ait.tooling.nativetools.client.datamodel.AbstractJSONDataModel;
import com.ait.tooling.nativetools.client.datamodel.ModelIDList;
public abstract class AbstractModelController implements IDataModelController, Function
{
private IDataModelStorage m_storage = null;
private final Collection m_emptied = Collections.unmodifiableCollection(new ArrayList(0));
private ArrayList> m_waiting = null;
protected AbstractModelController()
{
}
@Override
public final Collection getEmpty()
{
return m_emptied;
}
@Override
public final void getEmpty(final Consumer> callback)
{
callback.accept(getEmpty());
}
@Override
public final void prime(final Consumer callback)
{
queryModelsInCache(callback);
}
@Override
public final void values(final Consumer> callback)
{
prime(new Consumer()
{
@Override
public void accept(final Boolean value)
{
if (value)
{
callback.accept(m_storage.values());
}
else
{
getEmpty(callback);
}
}
});
}
@Override
public void create(final T model, final Consumer callback)
{
prime(new Consumer()
{
@Override
public void accept(final Boolean value)
{
if (value)
{
createModelInServerStorage(m_storage, model, new Consumer()
{
@Override
public void accept(final T result)
{
callback.accept(result);
}
});
}
else
{
callback.accept(null);
}
}
});
}
@Override
public void findByID(final String id, final Consumer callback)
{
prime(new Consumer()
{
@Override
public void accept(final Boolean value)
{
if (value)
{
callback.accept(m_storage.get(id));
}
else
{
callback.accept(null);
}
}
});
}
@Override
public void findByID(final ModelIDList list, final Consumer> callback)
{
prime(new Consumer()
{
@Override
public void accept(final Boolean value)
{
if (value)
{
final int size = list.size();
if (size > 0)
{
callback.accept(m_storage.get(list));
}
else
{
getEmpty(callback);
}
}
else
{
callback.accept(null);
}
}
});
}
private final void queryModelsInCache(final Consumer callback)
{
if (null != m_storage)
{
callback.accept(true);
}
else
{
if (null != m_waiting)
{
m_waiting.add(callback);
}
else
{
m_waiting = new ArrayList>();
m_waiting.add(callback);
queryModelsInServerStorage(makeClientStorage(), new Consumer>()
{
@Override
public void accept(final IDataModelStorage result)
{
if (null == result)
{
for (Consumer entry : m_waiting)
{
entry.accept(false);
}
m_waiting.clear();
m_waiting = null;
}
else
{
m_storage = result;
for (Consumer entry : m_waiting)
{
entry.accept(true);
}
m_waiting.clear();
m_waiting = null;
}
}
});
}
}
}
@Override
public void updateByID(final String id, final T model, final Consumer callback)
{
prime(new Consumer()
{
@Override
public void accept(final Boolean value)
{
if (value)
{
updateModelInServerStorage(m_storage, model, callback);
}
else
{
callback.accept(false);
}
}
});
}
final void setModelByID(final String id, final T model)
{
m_storage.put(id, model);
}
final T getModelByID(final String id)
{
return m_storage.get(id);
}
@Override
public void deleteByID(final String id, final Consumer callback)
{
deleteByID(new ModelIDList(id), callback);
}
@Override
public void deleteByID(final ModelIDList list, final Consumer callback)
{
prime(new Consumer()
{
@Override
public void accept(final Boolean value)
{
if (value)
{
deleteModelInServerStorage(m_storage, list, callback);
}
else
{
callback.accept(false);
}
}
});
}
protected NFastStringMapDataModelStorage makeClientStorage()
{
return new NFastStringMapDataModelStorage(getName(), this);
}
abstract protected void createModelInServerStorage(IDataModelStorage storage, T model, Consumer callback);
abstract protected void updateModelInServerStorage(IDataModelStorage storage, T model, Consumer callback);
abstract protected void deleteModelInServerStorage(IDataModelStorage storage, ModelIDList list, Consumer callback);
abstract protected void queryModelsInServerStorage(IDataModelStorage storage, Consumer> callback);
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy