/// <summary>Register an Annotator that can be created by the pool.</summary> /// <remarks> /// Register an Annotator that can be created by the pool. /// Note that factories are used here so that many possible annotators can /// be defined within the AnnotatorPool, but an Annotator is only created /// when one is actually needed. /// </remarks> /// <param name="name">The name to be associated with the Annotator.</param> /// <param name="props">The properties we are using to create the annotator</param> /// <param name="annotator"> /// A factory that creates an instance of the desired Annotator. /// This should be an instance of /// <see cref="Edu.Stanford.Nlp.Util.Lazy{E}.Cache{E}(Java.Util.Function.ISupplier{T})"/> /// , if we want /// the annotator pool to behave as a cache (i.e., evict old annotators /// when the GC requires it). /// </param> /// <returns>true if a new annotator was created; false if we reuse an existing one</returns> public virtual bool Register(string name, Properties props, Lazy <IAnnotator> annotator) { bool newAnnotator = false; string newSig = PropertiesUtils.GetSignature(name, props); lock (this.cachedAnnotators) { AnnotatorPool.CachedAnnotator oldAnnotator = this.cachedAnnotators[name]; if (oldAnnotator == null || !Objects.Equals(oldAnnotator.signature, newSig)) { // the new annotator uses different properties so we need to update! if (oldAnnotator != null) { // Try to get it from the global cache log.Debug("Replacing old annotator \"" + name + "\" with signature [" + oldAnnotator.signature + "] with new annotator with signature [" + newSig + "]"); } // Add the new annotator this.cachedAnnotators[name] = new AnnotatorPool.CachedAnnotator(newSig, annotator); // Unmount the old annotator Optional.OfNullable(oldAnnotator).FlatMap(null).IfPresent(null); // Register that we added an annotator newAnnotator = true; } } // nothing to do if an annotator with same name and signature already exists return(newAnnotator); }
/// <summary> /// <inheritDoc/> /// /// </summary> public override bool Equals(object o) { if (this == o) { return(true); } if (o == null || GetType() != o.GetType()) { return(false); } AnnotatorPool.CachedAnnotator that = (AnnotatorPool.CachedAnnotator)o; return(signature != null?signature.Equals(that.signature) : that.signature == null && (annotator != null ? annotator.Equals(that.annotator) : that.annotator == null)); }
/// <summary>Retrieve an Annotator from the pool.</summary> /// <remarks> /// Retrieve an Annotator from the pool. If the named Annotator has not yet /// been requested, it will be created. Otherwise, the existing instance of /// the Annotator will be returned. /// </remarks> /// <param name="name">The annotator to retrieve from the pool</param> /// <returns>The annotator</returns> /// <exception cref="System.ArgumentException">If the annotator cannot be created</exception> public virtual IAnnotator Get(string name) { lock (this) { AnnotatorPool.CachedAnnotator factory = this.cachedAnnotators[name]; if (factory != null) { return(factory.annotator.Get()); } else { throw new ArgumentException("No annotator named " + name); } } }