All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.m4rc310.gql.services.MFluxService Maven / Gradle / Ivy

The newest version!
package br.com.m4rc310.gql.services;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

import org.reactivestreams.Publisher;

import br.com.m4rc310.gql.dto.MUser;
import br.com.m4rc310.gql.location.dto.DtoGeolocation;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;

/**
 * The Class MFluxService.
 *
 * @author marcelo
 * @version $Id: $Id
 */
public class MFluxService {
	/** The registry. */
	protected final MMultiRegitry registry = new MMultiRegitry<>();

	private MUser user;
	
	private String ipClient;
	
	private DtoGeolocation geolocation;

	/**
	 * Publish.
	 *
	 * @param   the generic type
	 * @param type the type
	 * @param key  the key
	 * @return the publisher
	 */
	public  Publisher publish(Class type, Object key) {
		return publish(type, key, null);
	}

	/**
	 * 

* publish. *

* * @param type a {@link java.lang.Class} object * @param key a {@link java.lang.Object} object * @param defaultValue a T object * @param a T class * @return a {@link org.reactivestreams.Publisher} object */ public Publisher publish(Class type, Object key, T defaultValue) { String skey = makeId(type, key); return Flux.create(fs -> { registry.add(skey, fs.onDispose(() -> registry.remove(skey))); if (Objects.nonNull(defaultValue)) { fs.next(defaultValue); } }, FluxSink.OverflowStrategy.BUFFER); } /** *

* getSizeRegistries. *

* * @param type a {@link java.lang.Class} object * @return a {@link java.lang.Integer} object */ public Integer getSizeRegistries(Class type) { return registry.getSizeRegistries(type); } /** *

* publishList. *

* * @param type a {@link java.lang.Class} object * @param key a {@link java.lang.Object} object * @param defaultValue a {@link java.util.List} object * @param a T class * @return a {@link reactor.core.publisher.Flux} object */ public Flux> publishList(Class type, Object key, List defaultValue) { String skey = makeId(type, key); return Flux.create(fs -> { registry.add(skey, fs.onDispose(() -> registry.remove(skey))); fs.next(defaultValue); }, FluxSink.OverflowStrategy.BUFFER); } /** * In publish. * * @param type the type * @param key the key * @return true, if successful */ public boolean inPublish(Class type, Object key) { String skey = makeId(type, key); return registry.contains(skey); } /** * Call publish. * * @param the generic type * @param key the key * @param value the value * @throws java.lang.Exception the exception */ @SuppressWarnings("unchecked") public void callPublish(Object key, T value) throws Exception { if (value == null) { throw new Exception("Value is null"); } Class type = value.getClass(); if (!inPublish(type, key)) { throw new Exception("No published listener!"); } String skey = makeId(type, key); List> sinks = registry.get(skey); if (sinks != null) { sinks.forEach(sub -> ((FluxSink) sub).next(value)); } // registry.get(skey).forEach(sub -> sub.next(value)); } /** * Call publish from type. * * @param the generic type * @param value the value * @throws java.lang.Exception the exception */ public void callPublish(Class type, T value) throws Exception { List keys = registry.getKeys(type); for (String key : keys) { //System.out.println(key); callPublish(type, key, value); } // registry.getKeys(type).forEach((key) -> { // try { // } catch (Exception e) { // e.printStackTrace(); // } // }); } public List getKeys(Class type) { return registry.getKeys(type); } /** * Removes the publish. * * @param the generic type * @param type the type * @param key the key */ public void removePublish(Class type, String key) { String skey = makeId(type, key); registry.remove(skey); } /** * Call publish. * * @param the generic type * @param type the type * @param key the key * @param value the value * @throws java.lang.Exception the exception */ @SuppressWarnings("unchecked") public void callPublish(Class type, Object key, T value) throws Exception { String skey = String.format("%s", key); // makeId(type, key); List> sinks = registry.get(skey); if (sinks != null) { sinks.forEach(sub -> ((FluxSink) sub).next(value)); } } /** *

* callListPublish. *

* * @param type a {@link java.lang.Class} object * @param key a {@link java.lang.Object} object * @param list a {@link java.util.List} object * @param a T class */ @SuppressWarnings("unchecked") public void callListPublish(Class type, Object key, List list) { String skey = makeId(type, key); List> sinks = registry.get(skey); if (sinks != null) { sinks.forEach(sub -> ((FluxSink>) sub).next(list)); } } /** * Authenticated user. * * @return the m user */ // public MUser authenticatedUser() { // try { // return (MUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal(); // } catch (Exception e) { // return null; // } // } /** * Make id. * * @param type the type * @param key the key * @return the string */ private String makeId(Class type, Object key) { return String.format("%s-%s", type.getSimpleName(), key); } /** * Clone ato B. * * @param a the a * @param b the b */ public void cloneAtoB(Object a, Object b) { List fas = listAllFields(a); List fbs = listAllFields(b); fas.forEach(fa -> { try { fa.setAccessible(true); fbs.forEach(fb -> { fb.setAccessible(true); if (fa.getName().equals(fb.getName())) { try { Object value = fa.get(a); if (Objects.nonNull(value)) { try { if (Objects.isNull(fb.get(b))) { fb.set(b, value); } } catch (Exception e) { } } } catch (Exception e) { e.printStackTrace(); } } }); } catch (Exception e) { e.printStackTrace(); } }); // Class typeA = listAllFields(a); // Class typeB = b.getClass(); // listAllFields(a).forEach(fieldA -> { // try { // for(Field fieldA : typeA.getDeclaredFields()) { // fieldA.setAccessible(true); // // for(Field fieldB : typeB.getDeclaredFields()) { // // } // // // //log.info("{}", field.get(a)); // } // // // // Class typeB = b.getClass(); // // // // // Field fieldB = typeB.getField(fieldA.getName()); // if (Objects.nonNull(fieldB) && Objects.isNull(fieldB.get(b))) { // fieldB.setAccessible(true); // fieldB.set(b, fieldA.get(a)); // } // } catch (Exception e) { // e.printStackTrace(); // } // }); } /** * Gets the field from type. * * @param type the type * @param typeField the type field * @return the field from type */ public Field getFieldFromType(Class type, Class typeField) { List fields = new ArrayList<>(); fields.addAll(Arrays.asList(type.getFields())); fields.addAll(Arrays.asList(type.getDeclaredFields())); for (Field field : fields) { field.setAccessible(true); if (field.getType().getName().equals(typeField.getName())) { return field; } } ; return null; } /** * List all fields. * * @param o the o * @return the list */ private List listAllFields(Object o) { List fields = new ArrayList<>(); Class type = o.getClass(); fields.addAll(Arrays.asList(type.getFields())); fields.addAll(Arrays.asList(type.getDeclaredFields())); return fields; } public void setUser(MUser user) { this.user = user; } public MUser getUser() { return user; } public void setIPClient(String ipClient) { this.ipClient = ipClient; } public String getIPClient() { return ipClient; } public DtoGeolocation getGeolocation() { return geolocation; } public void setGeolocation(DtoGeolocation geolocation) { this.geolocation = geolocation; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy