How to create an event with integer and float type in Yahoo! S4 - stream-processing

Suppose that I want to create an event from a Java bean in Yahoo! S4. I can use the following code to do that:
import io.s4.client.Driver
import io.s4.client.Message
Driver d = new Driver(hostName, port);
Message m = new Message(streamName, clazz, string);
d.send(m);
Message Constructor Arguments:
streamName - any name for the stream
clazz - the fully qualified name of the event class for the stream
string - the string representation of the object
Question:
What would the string representation of the following object look like so that it can be used in an event?
public class CarType {
private String carNO;
private String tyreNO;
private double pressureReading;
private long Timer;
private boolean carMoving;
}

Related

Groovy GString fails to interpolate a variable when referenced by an interface

Summary
I have a very odd situation where a GString is incorrectly interpolating a variable as null even though the variable has a non-null value set. See the screenshot below. However, it interpolates correctly if I reference the variable by its actual class rather than by the interface.
PublicBinary is an interface
interface PublicBinary {
byte[] bytes
String publicKey
Person person
}
AdditionalPhoto is the concrete class that implements PublicBinary
class AdditionalPhoto implements PublicBinary, ToMap, DomainClass {
byte[] bytes
String publicKey
String externalURL
Person person
AdditionalPhotoType additionalPhotoType
...
}
Interpolation fails when I type the variable as its interface, PublicBinary
But interpolation works properly when I type the variable as its actual class, AdditionalPhoto, rather than as its interface, PublicBinary.
I cannot reproduce the behavior you are describing but the project at https://github.com/jeffbrown/tengritsandpapergstringinterpolation demonstrates an approach that works.
lib/src/main/groovy/tengritsandpapergstringinterpolation/PublicBinary.groovy
package tengritsandpapergstringinterpolation
interface PublicBinary {
byte[] bytes
String publicKey
Person person
}
lib/src/main/groovy/tengritsandpapergstringinterpolation/AdditionalPublicBinary.groovy
package tengritsandpapergstringinterpolation
class AdditionalPublicBinary implements PublicBinary {
byte[] bytes
String publicKey
String externalURL
Person person
}
lib/src/main/groovy/tengritsandpapergstringinterpolation/Library.groovy
package tengritsandpapergstringinterpolation
class Library {
String processByInterface(PublicBinary obj) {
return "Public Key Is [${obj.publicKey}]"
}
String processByConcreteType(AdditionalPublicBinary obj) {
return "Public Key Is [${obj.publicKey}]"
}
}
lib/src/test/groovy/tengritsandpapergstringinterpolation/LibraryTest.groovy
package tengritsandpapergstringinterpolation
import spock.lang.Specification
class LibraryTest extends Specification {
def "someLibraryMethod returns true"() {
setup:
def lib = new Library()
def binary = new AdditionalPublicBinary(publicKey: 'My Key Value')
expect:
lib.processByConcreteType(binary) == 'Public Key Is [My Key Value]'
lib.processByInterface(binary) == 'Public Key Is [My Key Value]'
}
}
One thing that is unusual is the way you have defined properties in both the interface and impl.
The problem is your interface. This is not the interface you are
intending, because it defines no getters (or maybe even setters) for
your property. It defines un-initialized, public, final properties.
The following shows the problem:
interface I1 {
String x
}
class X1 implements I1 {
}
println(new X1().x) // → null
If you decompile the "wrong" interface, you see what is going on:
public interface I {
public static final java.lang.String x;
}
While a "proper" interface will match your expectations:
interface I2 {
String getX()
}
class X2 implements I2 {
String x
}

Dart - assigning class to factory constructor

I have found an interesting (to me) place in Dart code:
factory Uri(
{String scheme,
String userInfo,
String host,
int port,
String path,
Iterable<String> pathSegments,
String query,
Map<String, dynamic /*String|Iterable<String>*/ > queryParameters,
String fragment}) = _Uri; // <==== here
and then:
class _Uri implements Uri {
...
}
It looks like the class _Uri is assigned to the factory constructor. I don't think I have read about it in the language tour or anywhere else. What is this 'technique' called? How does it work? Are there any special requirements for the factory constructor and the class for this to work?

How to ignore "too many parameters" on constructors that use injection

If you are using dependency injection it's totally normal that constructors have many parameters.
Is it possible that sonarlint doesn't show the "too many parameters" code smell warning for constructors that use injection?
In my case a Spring service needed a lot of other services in order to do its work. So rule squid:S00107 fired and gave me an error in the SonarCube. I found this article which proposed to use Lombok to work around the issue like so:
Before:
#Component
public class EventService {
private final String param1;
private final String param2;
private final String param3;
private final String param4;
private final String param5;
private final String param6;
private final String param7;
private final String param8;
private final String param9;
public EventService(String param1, String param2, String param3, String param4, String param5, String param6, String param7, String param8, String param9) {
this.param1 = param1;
this.param2 = param2;
this.param3 = param3;
this.param4 = param4;
this.param5 = param5;
this.param6 = param6;
this.param7 = param7;
this.param8 = param8;
this.param9 = param9;
}
...
After:
#Component
#RequiredArgsConstructor
public class EventService {
private final String param1;
private final String param2;
private final String param3;
private final String param4;
private final String param5;
private final String param6;
private final String param7;
private final String param8;
private final String param9;
...
As other posters noted before, too many arguments for a method or constructor show that somehow the separation of concerns principle has been violated. It is better to go with the following solutions:
in case of a constructor: a builder pattern
in case of a method: with a request parameter and a builder if has too many parameters.
This avoids confusion of parameters, i.e. "Was the x, y or the z first argument?"
If all tries fail annotate #SuppressWarnings("squid:S00107") on top of the method / constructor you want to ignore. This can only be last resource.

neo4j java node dynamic properties

I am trying to create nodes of a specific type with properties which can be dynamic .
For Example : I can create a Person node with name,age,address properties. But these need not be the only properties when I create another Person node. This node can have name,age,address and an additional property salary. Using spring data or query DSL needs me to create Java POJO class Person with fixed number of instance variables name,age and address .
#NodeEntity
public class Person {
#GraphId private Long id;
private String name;
private String age;
private String address;
}
I cannot add a dynamic property for salary for another Person node. Is there a way I can achieve this ?
Dynamic properties are not supported in Neo4j-OGM at the moment (see https://jira.spring.io/browse/DATAGRAPH-555)
If you only interact with your graph via the OGM and do not have to query on individual dynamic properties, you could try a Map of properties with a custom Converter, that converts this Map to a String (like json). The OGM will then use this converter to serialize the map to and from the graph.
Note that because the values are squashed into a String, it is now not trivial to query on an individual dynamic property.
To create a custom converter you need to implement org.neo4j.ogm.typeconversion.AttributeConverter and provide the implementation to convert from a Map to String.
Then, annotate your map property in your domain entity like this:
#Convert(MoneyConverter.class)
Edit:
As pointed out by Michael, if the salary is the only extra optional property, then it makes sense to have this property but set it only when it has a value. Dynamic properties are overkill in this case. You may want to use dynamic properties when you have an unknown and arbitrary set of properties to be persisted with the node
You can workaround the limitations by creating a CompositeAttributeConverter saving each dynamic property in the graph (not only as JSON-String wich cannot be queried well - as mentioned by luanne in the accepted answer)
import java.lang.reflect.Field;
import java.util.*;
import org.neo4j.ogm.typeconversion.CompositeAttributeConverter;
public abstract class DynamicPropertiesConverter implements CompositeAttributeConverter<Map<String, ?>> {
private Set<String> blacklist;
public DynamicPropertiesConverter(Class<?> clazz) {
blacklist = new HashSet<>();
addAllFields(clazz);
}
public DynamicPropertiesConverter(Set<String> blacklist) {
this.blacklist = blacklist;
}
public void addAllFields(Class<?> type) {
for (Field field : type.getDeclaredFields()) {
blacklist.add(field.getName());
}
if (type.getSuperclass() != null) {
addAllFields(type.getSuperclass());
}
}
#Override
public Map<String, ?> toGraphProperties(Map<String, ?> value) {
Map<String, ?> result = new HashMap<>(value);
result.keySet().removeAll(blacklist);
return result;
}
#Override
public Map<String, ?> toEntityAttribute(Map<String, ?> value) {
return toGraphProperties(value);
}
}
Now you can create a special version of this converter:
public class DynamicNodePropertiesConverter extends DynamicPropertiesConverter {
public DynamicNodePropertiesConverter() {
super(Node.class);
}
}
And use it like this:
import java.util.Map;
import DynamicNodePropertiesConverter;
import org.neo4j.ogm.annotation.NodeEntity;
import org.neo4j.ogm.annotation.Relationship;
import org.neo4j.ogm.annotation.typeconversion.Convert;
#NodeEntity
public class Node {
#Convert(DynamicNodePropertiesConverter.class)
private Map<String, Object> data;
/* getter and setter */
}

Map by Class in Dart

I am porting some Java-code to Dart and it heavily uses these kinds of maps:
Map<Class<? extends SomeClass>, SomeOtherClass> map = new HashMap<>();
At the moment this seems to be impossible in dart. I am aware that there is a proposal to introduce first level types: http://news.dartlang.org/2012/06/proposal-for-first-class-types-in-dart.html which would introduce
class Type {
#native String toString();
String descriptor(){...} // return the simple name of the type
}
So until this proposal gets implemented I have created following class:
class Type {
final String classname;
const Type(this.classname);
String descriptor() => classname;
}
and the classes where I need it have a simple get-method
abstract Type get type();
That way I can use my Type just like I would use the real Type and to switch later I'd just have to delete my workaround.
My question: Is there some dart-way of doing this kind of mapping (which I am not seeing) or is the way I do it a reasonable workaround until the real Type class gets introduced?
Update for Dart 1.0
It can be done this way:
var map = new Map<Type, SomeOtherClass>();
// either
map[SomeOtherClass] = new SomeOtherClass();
// or
var instance = new SomeOtherClass();
map[instance.runtimeType] = instance;
Update: this construction is not currently doable in Dart
Map<Class<? extends SomeClass>, SomeOtherClass>
you will have to wait for .type/.class to arrive for an elegant solution to this (lots of us Dartisans are hoping that this will arrive sooner rather than later). However for the simpler case
Map<? extends SomeClass, SomeOtherClass>
You can just do
Map<SomeClass, SomeOtherClass> aMap;
as in Dart any class that extends SomeClass is also going to be a valid SomeClass. For example if you run the following code in checked mode:
main() {
Map<Test, String> aMap = new HashMap<Test, String>();
var test = new Test("hello");
var someTest = new SomeTest("world");
var notATest = new NotATest();
aMap[test] = test.msg;
aMap[someTest] = someTest.msg;
aMap[notATest] = "this fails";
}
class Test implements Hashable {
Test(this.msg);
int hashCode() => msg.hashCode();
final String msg;
}
class SomeTest extends Test {
SomeTest(String message): super(message);
}
class NotATest implements Hashable {
int hashCode() => 1;
}
then you you will get the error:
type 'NotATest' is not a subtype of type 'Test' of 'key'.

Resources