jacob-project icon indicating copy to clipboard operation
jacob-project copied to clipboard

Proposed IterableEnumVariant class.

Open EJP286CRSKW opened this issue 1 year ago • 4 comments

I propose this addition to the com.jacob.com package. It is an Iterable and Stream-able subclass of EnumVariant.

IteratableEnumVariant class.

package com.jacob.com;

import java.util.Iterator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

/**
 * Iterable and streaming EnumVariant.
 * 
 * @author Esmond Pitt
 * @since 1.21
 */
public class IterableEnumVariant extends EnumVariant implements Iterable<Variant>
{
    /**
     * Construct an instance.
     * 
     * @param variant Variant from which to obtain the enumeration.
     */
    public IterableEnumVariant(Variant variant)
    {
        super(variant.m_pVariant);
    }
    
    /**
     * {@inheritDoc}
     * <p>
     * This implementation resets the EnumVariant,
     * so that <code>iterator()</code> can be called multiple times,
     * as can {@link #stream}.
     */
    @Override
    public Iterator<Variant>   iterator()
    {
        super.Reset();
        return new Iterator<Variant>()
        {
            @Override
            public boolean  hasNext()
            {
                return hasMoreElements();
            }
            
            @Override
            public Variant  next()
            {
                return nextElement();
            }
        };
    }
    
    /**
     * Get a Stream from this object.
     * 
     * @return Stream&lt;Variant&gt;.
     */
    public Stream<Variant> stream()
    {
        return StreamSupport.stream(this.spliterator(), false);
    }
}

(or better still fold this into EnumVariant.

EJP286CRSKW avatar Aug 24 '23 06:08 EJP286CRSKW

Sample code?

freemansoft avatar Sep 04 '23 17:09 freemansoft

Sure. Executing a query:

Stream
    .of(wmiServer)
    .map(w -> w.invoke("ExecQuery", new Variant(query)))
    .map(Variant::toDispatch)
    .map(IterableActiveXComponent::new)
    .flatMap(IterableActiveXComponent::stream)
// .peek(System.out::println)
   .forEach
   (
       a ->
       {
//        System.out.println("query result="+a.getProperty("Name"));
       }
   )
   ;

EJP286CRSKW avatar Sep 04 '23 23:09 EJP286CRSKW

  1. The example uses IterableActiveXComponent which does not exist in the current code base
  2. Could you provide the sample query

freemansoft avatar Mar 19 '24 00:03 freemansoft

Added to EnumVariant https://github.com/freemansoft/jacob-project/commit/75124c90d754f5884f909030094215a0584cf256

Still need sample code or the IterableActiveXComponent

freemansoft avatar Mar 19 '24 02:03 freemansoft