Java tutorial
package edu.byu.softwareDistribution.web.workflow; import edu.byu.softwareDist.domain.License; import edu.byu.softwareDist.domain.LicenseSet; import edu.byu.softwareDist.domain.licenses.UniqueKeyLicense; import edu.byu.softwareDist.domain.licenses.UnlimitedWithNoKeyLicense; import scala.Function0; import java.io.Serializable; import java.util.*; import static java.util.Collections.unmodifiableList; import static org.springframework.util.StringUtils.hasText; /** * @author Tyler Southwick (tyler_southwick@byu.edu) */ public class Licenses implements Serializable, Iterable<License> { private final static long serialVersionUID = 8L; private List<License> licenses = new ArrayList<License>(); private List<License> oldLicenses = new LinkedList<License>(); private LicenseSet licenseSet = new LicenseSet(); private Function0<? extends License> createLicense; private License currentLicense; private Class<? extends License> oldClass = null; private boolean canHaveLicenses; public void addLicense(License license) { licenses.add(license); } public List<License> getLicenses() { return unmodifiableList(licenses); } public List<License> getOldLicenses() { return oldLicenses; } public void setOldLicenses(List<License> oldLicenses) { this.oldLicenses = oldLicenses; } public LicenseSet getLicenseSet() { return licenseSet; } public void setLicenseSet(LicenseSet licenseSet) { this.licenseSet = licenseSet; } public void removeLicense(License license) { licenses.remove(license); } public void setKeys(String keys) { for (String s : keys.split("\n")) { String key = s.trim(); if (hasText(key)) { addLicense(new UniqueKeyLicense(key)); } } } public String getKeys() { return null; } public License getLicense() { assert createLicense != null : "There is no create license closure"; if (currentLicense == null) { currentLicense = (License) createLicense.apply(); } assert currentLicense != null; return currentLicense; } public void setLicense(License license) { assert false; } public void clear() { licenses.clear(); } public <L extends License> void update(Class<L> klass, Function0<L> c) { update(klass); createLicense = c; } @SuppressWarnings("unchecked") public <L extends License> void update(Class<L> klass) { canHaveLicenses = !UnlimitedWithNoKeyLicense.class.isAssignableFrom(klass); if (klass.equals(oldClass)) { return; } licenses = extract1((Class<License>) klass); currentLicense = null; oldClass = klass; } private <L extends License> List<License> extract1(final Class<License> klass) { if (licenses == null || licenses.isEmpty()) return new ArrayList<License>(0); final List<License> result = new ArrayList<License>(licenses.size()); for (final License l : licenses) { if (klass.isInstance(l)) { result.add(l); } } return result; } public boolean isEmpty() { return licenses.isEmpty(); } public void addCurrentLicense() { if (currentLicense == null) { return; } addLicense(currentLicense); currentLicense = null; } public void removeLicense(int number) { licenses.remove(number - 1); } public void setRemoveLicense(int number) { removeLicense(number); } public int getSize() { return size(); } public int size() { return licenses.size(); } @Override public Iterator<License> iterator() { return new MyIterator(); } public List<License> getAllLicenses() { final List<License> result = new ArrayList<License>( (oldLicenses == null ? 0 : oldLicenses.size()) + (licenses == null ? 0 : licenses.size())); if (oldLicenses != null) { for (final License ol : oldLicenses) { result.add(ol); } } if (licenses != null) { for (final License l : licenses) { result.add(l); } } return new ArrayList<License>(new TreeSet<License>(result)); } public List<String> licenseKeys() { final List<String> result = new ArrayList<String>( (oldLicenses == null ? 0 : oldLicenses.size()) + (licenses == null ? 0 : licenses.size())); if (result.size() == 0) return result; if (oldLicenses != null) { for (final License ol : oldLicenses) { result.add(ol.getLicenseKey()); } } if (licenses != null) { for (final License l : licenses) { result.add(l.getLicenseKey()); } } return new ArrayList<String>(new TreeSet<String>(result)); } public int getQtyAvailable() { int count = 0; for (License license : this) { count += license.getQtyAvailable(); } return count; } public int getMaxQty() { int count = 0; for (License license : this) { count += license.getMaxQty(); } return count; } public boolean isCanHaveLicenses() { return canHaveLicenses; } private class MyIterator implements Iterator<License> { private final Iterator<Iterator<License>> iters; private volatile Iterator<License> curIter; private volatile Iterator<License> removeIter; private MyIterator() { final List<Iterator<License>> temp = new ArrayList<Iterator<License>>(2); if (oldLicenses != null) temp.add(oldLicenses.iterator()); if (licenses != null) temp.add(licenses.iterator()); iters = temp.iterator(); } public void getCurIter() { if (curIter == null) { if (iters.hasNext()) curIter = iters.next(); } } public void getNextIter() { if (iters.hasNext()) curIter = iters.next(); else curIter = null; } @Override public boolean hasNext() { getCurIter(); if (curIter == null) return false; if (curIter.hasNext()) return true; getNextIter(); return hasNext(); } @Override public License next() { if (hasNext()) { removeIter = curIter; return curIter.next(); } throw new NoSuchElementException(); } @Override public void remove() { if (removeIter != null) { removeIter.remove(); removeIter = null; } else { throw new IllegalStateException(); } } } }