service-layer local type inference changes (#1034)

local variable type inference for service-layer design patterns
This commit is contained in:
GVSharma 2019-10-26 21:05:32 +05:30 committed by Ilkka Seppälä
parent 70ddeaa194
commit f7a53f2d17
5 changed files with 45 additions and 45 deletions

View File

@ -79,24 +79,24 @@ public class App {
*/ */
public static void initData() { public static void initData() {
// spells // spells
Spell spell1 = new Spell("Ice dart"); var spell1 = new Spell("Ice dart");
Spell spell2 = new Spell("Invisibility"); var spell2 = new Spell("Invisibility");
Spell spell3 = new Spell("Stun bolt"); var spell3 = new Spell("Stun bolt");
Spell spell4 = new Spell("Confusion"); var spell4 = new Spell("Confusion");
Spell spell5 = new Spell("Darkness"); var spell5 = new Spell("Darkness");
Spell spell6 = new Spell("Fireball"); var spell6 = new Spell("Fireball");
Spell spell7 = new Spell("Enchant weapon"); var spell7 = new Spell("Enchant weapon");
Spell spell8 = new Spell("Rock armour"); var spell8 = new Spell("Rock armour");
Spell spell9 = new Spell("Light"); var spell9 = new Spell("Light");
Spell spell10 = new Spell("Bee swarm"); var spell10 = new Spell("Bee swarm");
Spell spell11 = new Spell("Haste"); var spell11 = new Spell("Haste");
Spell spell12 = new Spell("Levitation"); var spell12 = new Spell("Levitation");
Spell spell13 = new Spell("Magic lock"); var spell13 = new Spell("Magic lock");
Spell spell14 = new Spell("Summon hell bat"); var spell14 = new Spell("Summon hell bat");
Spell spell15 = new Spell("Water walking"); var spell15 = new Spell("Water walking");
Spell spell16 = new Spell("Magic storm"); var spell16 = new Spell("Magic storm");
Spell spell17 = new Spell("Entangle"); var spell17 = new Spell("Entangle");
SpellDao spellDao = new SpellDaoImpl(); var spellDao = new SpellDaoImpl();
spellDao.persist(spell1); spellDao.persist(spell1);
spellDao.persist(spell2); spellDao.persist(spell2);
spellDao.persist(spell3); spellDao.persist(spell3);
@ -116,64 +116,64 @@ public class App {
spellDao.persist(spell17); spellDao.persist(spell17);
// spellbooks // spellbooks
SpellbookDao spellbookDao = new SpellbookDaoImpl(); var spellbookDao = new SpellbookDaoImpl();
Spellbook spellbook1 = new Spellbook("Book of Orgymon"); var spellbook1 = new Spellbook("Book of Orgymon");
spellbookDao.persist(spellbook1); spellbookDao.persist(spellbook1);
spellbook1.addSpell(spell1); spellbook1.addSpell(spell1);
spellbook1.addSpell(spell2); spellbook1.addSpell(spell2);
spellbook1.addSpell(spell3); spellbook1.addSpell(spell3);
spellbook1.addSpell(spell4); spellbook1.addSpell(spell4);
spellbookDao.merge(spellbook1); spellbookDao.merge(spellbook1);
Spellbook spellbook2 = new Spellbook("Book of Aras"); var spellbook2 = new Spellbook("Book of Aras");
spellbookDao.persist(spellbook2); spellbookDao.persist(spellbook2);
spellbook2.addSpell(spell5); spellbook2.addSpell(spell5);
spellbook2.addSpell(spell6); spellbook2.addSpell(spell6);
spellbookDao.merge(spellbook2); spellbookDao.merge(spellbook2);
Spellbook spellbook3 = new Spellbook("Book of Kritior"); var spellbook3 = new Spellbook("Book of Kritior");
spellbookDao.persist(spellbook3); spellbookDao.persist(spellbook3);
spellbook3.addSpell(spell7); spellbook3.addSpell(spell7);
spellbook3.addSpell(spell8); spellbook3.addSpell(spell8);
spellbook3.addSpell(spell9); spellbook3.addSpell(spell9);
spellbookDao.merge(spellbook3); spellbookDao.merge(spellbook3);
Spellbook spellbook4 = new Spellbook("Book of Tamaex"); var spellbook4 = new Spellbook("Book of Tamaex");
spellbookDao.persist(spellbook4); spellbookDao.persist(spellbook4);
spellbook4.addSpell(spell10); spellbook4.addSpell(spell10);
spellbook4.addSpell(spell11); spellbook4.addSpell(spell11);
spellbook4.addSpell(spell12); spellbook4.addSpell(spell12);
spellbookDao.merge(spellbook4); spellbookDao.merge(spellbook4);
Spellbook spellbook5 = new Spellbook("Book of Idores"); var spellbook5 = new Spellbook("Book of Idores");
spellbookDao.persist(spellbook5); spellbookDao.persist(spellbook5);
spellbook5.addSpell(spell13); spellbook5.addSpell(spell13);
spellbookDao.merge(spellbook5); spellbookDao.merge(spellbook5);
Spellbook spellbook6 = new Spellbook("Book of Opaen"); var spellbook6 = new Spellbook("Book of Opaen");
spellbookDao.persist(spellbook6); spellbookDao.persist(spellbook6);
spellbook6.addSpell(spell14); spellbook6.addSpell(spell14);
spellbook6.addSpell(spell15); spellbook6.addSpell(spell15);
spellbookDao.merge(spellbook6); spellbookDao.merge(spellbook6);
Spellbook spellbook7 = new Spellbook("Book of Kihione"); var spellbook7 = new Spellbook("Book of Kihione");
spellbookDao.persist(spellbook7); spellbookDao.persist(spellbook7);
spellbook7.addSpell(spell16); spellbook7.addSpell(spell16);
spellbook7.addSpell(spell17); spellbook7.addSpell(spell17);
spellbookDao.merge(spellbook7); spellbookDao.merge(spellbook7);
// wizards // wizards
WizardDao wizardDao = new WizardDaoImpl(); var wizardDao = new WizardDaoImpl();
Wizard wizard1 = new Wizard("Aderlard Boud"); var wizard1 = new Wizard("Aderlard Boud");
wizardDao.persist(wizard1); wizardDao.persist(wizard1);
wizard1.addSpellbook(spellbookDao.findByName("Book of Orgymon")); wizard1.addSpellbook(spellbookDao.findByName("Book of Orgymon"));
wizard1.addSpellbook(spellbookDao.findByName("Book of Aras")); wizard1.addSpellbook(spellbookDao.findByName("Book of Aras"));
wizardDao.merge(wizard1); wizardDao.merge(wizard1);
Wizard wizard2 = new Wizard("Anaxis Bajraktari"); var wizard2 = new Wizard("Anaxis Bajraktari");
wizardDao.persist(wizard2); wizardDao.persist(wizard2);
wizard2.addSpellbook(spellbookDao.findByName("Book of Kritior")); wizard2.addSpellbook(spellbookDao.findByName("Book of Kritior"));
wizard2.addSpellbook(spellbookDao.findByName("Book of Tamaex")); wizard2.addSpellbook(spellbookDao.findByName("Book of Tamaex"));
wizardDao.merge(wizard2); wizardDao.merge(wizard2);
Wizard wizard3 = new Wizard("Xuban Munoa"); var wizard3 = new Wizard("Xuban Munoa");
wizardDao.persist(wizard3); wizardDao.persist(wizard3);
wizard3.addSpellbook(spellbookDao.findByName("Book of Idores")); wizard3.addSpellbook(spellbookDao.findByName("Book of Idores"));
wizard3.addSpellbook(spellbookDao.findByName("Book of Opaen")); wizard3.addSpellbook(spellbookDao.findByName("Book of Opaen"));
wizardDao.merge(wizard3); wizardDao.merge(wizard3);
Wizard wizard4 = new Wizard("Blasius Dehooge"); var wizard4 = new Wizard("Blasius Dehooge");
wizardDao.persist(wizard4); wizardDao.persist(wizard4);
wizard4.addSpellbook(spellbookDao.findByName("Book of Kihione")); wizard4.addSpellbook(spellbookDao.findByName("Book of Kihione"));
wizardDao.merge(wizard4); wizardDao.merge(wizard4);
@ -183,7 +183,7 @@ public class App {
* Query the data * Query the data
*/ */
public static void queryData() { public static void queryData() {
MagicService service = var service =
new MagicServiceImpl(new WizardDaoImpl(), new SpellbookDaoImpl(), new SpellDaoImpl()); new MagicServiceImpl(new WizardDaoImpl(), new SpellbookDaoImpl(), new SpellDaoImpl());
LOGGER.info("Enumerating all wizards"); LOGGER.info("Enumerating all wizards");
for (Wizard w : service.findAllWizards()) { for (Wizard w : service.findAllWizards()) {

View File

@ -57,12 +57,12 @@ public abstract class DaoBaseImpl<E extends BaseEntity> implements Dao<E> {
@Override @Override
public E find(Long id) { public E find(Long id) {
Session session = getSessionFactory().openSession(); var session = getSessionFactory().openSession();
Transaction tx = null; Transaction tx = null;
E result = null; E result = null;
try { try {
tx = session.beginTransaction(); tx = session.beginTransaction();
Criteria criteria = session.createCriteria(persistentClass); var criteria = session.createCriteria(persistentClass);
criteria.add(Restrictions.idEq(id)); criteria.add(Restrictions.idEq(id));
result = (E) criteria.uniqueResult(); result = (E) criteria.uniqueResult();
tx.commit(); tx.commit();
@ -79,7 +79,7 @@ public abstract class DaoBaseImpl<E extends BaseEntity> implements Dao<E> {
@Override @Override
public void persist(E entity) { public void persist(E entity) {
Session session = getSessionFactory().openSession(); var session = getSessionFactory().openSession();
Transaction tx = null; Transaction tx = null;
try { try {
tx = session.beginTransaction(); tx = session.beginTransaction();
@ -97,7 +97,7 @@ public abstract class DaoBaseImpl<E extends BaseEntity> implements Dao<E> {
@Override @Override
public E merge(E entity) { public E merge(E entity) {
Session session = getSessionFactory().openSession(); var session = getSessionFactory().openSession();
Transaction tx = null; Transaction tx = null;
E result = null; E result = null;
try { try {
@ -117,7 +117,7 @@ public abstract class DaoBaseImpl<E extends BaseEntity> implements Dao<E> {
@Override @Override
public void delete(E entity) { public void delete(E entity) {
Session session = getSessionFactory().openSession(); var session = getSessionFactory().openSession();
Transaction tx = null; Transaction tx = null;
try { try {
tx = session.beginTransaction(); tx = session.beginTransaction();
@ -135,7 +135,7 @@ public abstract class DaoBaseImpl<E extends BaseEntity> implements Dao<E> {
@Override @Override
public List<E> findAll() { public List<E> findAll() {
Session session = getSessionFactory().openSession(); var session = getSessionFactory().openSession();
Transaction tx = null; Transaction tx = null;
List<E> result = null; List<E> result = null;
try { try {

View File

@ -70,14 +70,14 @@ public class MagicServiceImpl implements MagicService {
@Override @Override
public List<Wizard> findWizardsWithSpellbook(String name) { public List<Wizard> findWizardsWithSpellbook(String name) {
Spellbook spellbook = spellbookDao.findByName(name); var spellbook = spellbookDao.findByName(name);
return new ArrayList<>(spellbook.getWizards()); return new ArrayList<>(spellbook.getWizards());
} }
@Override @Override
public List<Wizard> findWizardsWithSpell(String name) { public List<Wizard> findWizardsWithSpell(String name) {
Spell spell = spellDao.findByName(name); var spell = spellDao.findByName(name);
Spellbook spellbook = spell.getSpellbook(); var spellbook = spell.getSpellbook();
return new ArrayList<>(spellbook.getWizards()); return new ArrayList<>(spellbook.getWizards());
} }
} }

View File

@ -41,9 +41,9 @@ public class SpellDaoImpl extends DaoBaseImpl<Spell> implements SpellDao {
public Spell findByName(String name) { public Spell findByName(String name) {
Transaction tx = null; Transaction tx = null;
Spell result = null; Spell result = null;
try (Session session = getSessionFactory().openSession()) { try (var session = getSessionFactory().openSession()) {
tx = session.beginTransaction(); tx = session.beginTransaction();
Criteria criteria = session.createCriteria(persistentClass); var criteria = session.createCriteria(persistentClass);
criteria.add(Restrictions.eq("name", name)); criteria.add(Restrictions.eq("name", name));
result = (Spell) criteria.uniqueResult(); result = (Spell) criteria.uniqueResult();
tx.commit(); tx.commit();

View File

@ -39,12 +39,12 @@ public class SpellbookDaoImpl extends DaoBaseImpl<Spellbook> implements Spellboo
@Override @Override
public Spellbook findByName(String name) { public Spellbook findByName(String name) {
Session session = getSessionFactory().openSession(); var session = getSessionFactory().openSession();
Transaction tx = null; Transaction tx = null;
Spellbook result = null; Spellbook result = null;
try { try {
tx = session.beginTransaction(); tx = session.beginTransaction();
Criteria criteria = session.createCriteria(persistentClass); var criteria = session.createCriteria(persistentClass);
criteria.add(Restrictions.eq("name", name)); criteria.add(Restrictions.eq("name", name));
result = (Spellbook) criteria.uniqueResult(); result = (Spellbook) criteria.uniqueResult();
result.getSpells().size(); result.getSpells().size();