/** * Sets the Listener that will be triggered for completed write operations on the matching * {@link Sink} and upon close and exhaustion. * * <p>Note that this Listener MAY NOT under any circumstances throw an exception. If it does so, * it will NOT be propagated outside of the Pipe, since the concurrent nature of this interface * makes it unreasonable to try to choose a relevant stack to propagate such an exception up * through. */ public void setListener(Listener<ReadHead<$T>> $el) { DataPipe.this.$el = $el; // it's possible that there wasn't a listener before this, and we need to make sure we fire an // event now in case there aren't any more writes forthcoming for a while (if indeed ever). // this can be "spurious", since it doesn't actually come as news of a write, but it's // terribly important not to ignore this. boolean $mustSpur = false; lockWrite(); if (SRC.hasNext()) $mustSpur = true; unlockWrite(); /* we prefer to release locks before we let the listener go on a tear, just as a matter of best/simplest practice. */ if ($mustSpur) invokeListener(DataPipe.this.$el); }
public static <SRC, DEST> DEST convert(SRC sourceObject, Class<DEST> toClass) throws ConversionException { if (sourceObject == null) return null; try { DEST destinationObject = toClass.newInstance(); List<Field> fields = getAllFields(sourceObject.getClass()); for (Field field : fields) { Method sourceGetter = getGetter(sourceObject.getClass(), field); Method destinationSetter = getSetter(toClass, field); if (sourceGetter == null) continue; try { Object toSet = sourceGetter.invoke(sourceObject); if (destinationSetter != null && toSet != null) { // if setting object or primitive type Class<?> parameterType = destinationSetter.getParameterTypes()[0]; if (TypeUtils.isInstance(toSet, parameterType)) { // if toSet is castable to parameter type, set it directly destinationSetter.invoke(destinationObject, toSet); } else if (toSet != null) { // if toSet is an unknown object, translate it via recursion destinationSetter.invoke( destinationObject, convert(toSet, parameterType)); // recursion } } } catch (InvocationTargetException e) { e.printStackTrace(); // don't throw to continue in mapping other fields } } return destinationObject; } catch (IllegalAccessException | IllegalArgumentException | InstantiationException e) { e.printStackTrace(); throw new ConversionException(e); } }
private void extractValues(String elementName, XmlPullParser pullParser) throws IOException, XmlPullParserException { this.repeatGap = REPEAT_GAP_DEFAULT * displayModel.getScaleFactor(); this.repeatStart = REPEAT_START_DEFAULT * displayModel.getScaleFactor(); for (int i = 0; i < pullParser.getAttributeCount(); ++i) { String name = pullParser.getAttributeName(i); String value = pullParser.getAttributeValue(i); if (SRC.equals(name)) { this.src = value; } else if (DISPLAY.equals(name)) { this.display = Display.fromString(value); } else if (DY.equals(name)) { this.dy = Float.parseFloat(value) * displayModel.getScaleFactor(); } else if (ALIGN_CENTER.equals(name)) { this.alignCenter = Boolean.parseBoolean(value); } else if (CAT.equals(name)) { this.category = value; } else if (PRIORITY.equals(name)) { this.priority = Integer.parseInt(value); } else if (REPEAT.equals(name)) { this.repeat = Boolean.parseBoolean(value); } else if (REPEAT_GAP.equals(name)) { this.repeatGap = Float.parseFloat(value) * displayModel.getScaleFactor(); } else if (REPEAT_START.equals(name)) { this.repeatStart = Float.parseFloat(value) * displayModel.getScaleFactor(); } else if (ROTATE.equals(name)) { this.rotate = Boolean.parseBoolean(value); } else if (SYMBOL_HEIGHT.equals(name)) { this.height = XmlUtils.parseNonNegativeInteger(name, value) * displayModel.getScaleFactor(); } else if (SYMBOL_PERCENT.equals(name)) { this.percent = XmlUtils.parseNonNegativeInteger(name, value); } else if (SYMBOL_SCALING.equals(name)) { this.scaling = fromValue(value); } else if (SYMBOL_WIDTH.equals(name)) { this.width = XmlUtils.parseNonNegativeInteger(name, value) * displayModel.getScaleFactor(); } else { throw XmlUtils.createXmlPullParserException(elementName, name, value, i); } } }
/** * Closes the pipe. No more data will be allowed to be written to this Pipe's WriteHead. Any * blocked {@link DataPipe.Source#readAll()} will return whatever data they can, and other * blocked {@link DataPipe.Source#read()} will immediately return null following this call even * if they cannot get data (these two processes still happen in fair order). All subsequent * reads (blocking or nonblocking) will either return data immediately as long as any is still * buffered in the pipe, and then forevermore immediately return null once all buffered data is * depleted. */ public void close() { SRC.close(); }