Akka Streams GraphStage - java

Using GraphStage is recommended in Akka Streams, but I could not find any documentation on using the getStageActor() method in Java (all of the documentation that I have found used Scala).
How can I convert the following code to Java?
lazy val self: StageActor = getStageActor(onMessage)
and
private def onMessage(x: (ActorRef, Any)): Unit =
{
x match {
case (_, msg: String) =>
log.info("received msg, queueing: {} ", msg)
messages = messages.enqueue(msg)
pump()
}
}

According to the getStageActor method documentation, it accepts a value of type
scala.Function1<scala.Tuple2<ActorRef,java.lang.Object>, scala.runtime.BoxedUnit>
which in Scala looks like
((ActorRef, AnyRef)) => Unit
In Java this type would be semantically equivalent (using the Function interface) to
Function<Tuple<ActorRef, Object>, Void>
where Tuple<A, B> is a class which contains two values of types A and B.
Therefore, to call the getStageActor method, you need to create a value of the aforementioned type. You can do it directly by constructing an instance of a class extending AbstractFunction1:
import scala.Function1;
import scala.Tuple2;
import scala.runtime.AbstractFunction1;
import scala.runtime.BoxedUnit;
getStateActor(new AbstractFunction1<Tuple2<ActorRef, Object>, BoxedUnit>() {
#Override
public BoxedUnit apply(Tuple2<ActorRef, Object> args) {
return BoxedUnit.UNIT;
}
});
If you use Java 8, there are syntactically nicer ways to do it using lambda expressions.
If you use Scala 2.12+, then scala.Function1 is a functional interface, and you can use lambda expressions directly:
getStateActor((args: Tuple2<ActorRef, Object>) -> BoxedUnit.UNIT);
If you use an older version of Scala, then due to the way traits are compiled, Function1 is not a functional interface, and you will need to use the scala-java8-compat library. With it, the code looks like
import static scala.compat.java8.JFunction.*;
getStateActor(func((args: Tuple2<ActorRef, Object>) -> BoxedUnit.UNIT));
Then, to implement the logic of the function, you can access elements of the tuple using the _1() and _2() methods:
(args: Tuple2<ActorRef, Object>) -> {
Object msg = args._2();
if (msg instanceof String) {
log.info("received msg, queueing: {} ", msg);
messages = messages.enqueue((String) msg);
pump();
}
return BoxedUnit.UNIT;
}
This is a direct translation of the logic that you wanted to convert.

I have not used getStageActor() before, hence can't provide much help there. As to code conversion from Scala to Java in general, if requirement allows, package a jar containing the classes of the Scala version for the Java app to use; otherwise consider using a Java decompiler (e.g. cfr, procyon) to decompile your Scala-compiled classes and refine the decompiled Java code as needed.
For example decompiling the following dummy Scala code would help reveal a skeletal Java way of lazy val and pattern matching:
class Foo {
lazy val self = dummy(bar(_: String))
def dummy(u: Unit) = 1
private def bar(x: String): Unit = {
x match {
case "blah" => println(s"x = $x")
}
}
}
As shown in the decompiled code below, lazy val is done in Java with the value wrapped inside a synchronized block with a bitmap$0 boolean flag, and pattern matching gets converted to if and MatchError exception:
$ java -jar /path/to/decompiler/cfr_0_125.jar Foo.class
private int self;
private volatile boolean bitmap$0;
private int self$lzycompute() {
Foo foo = this;
synchronized (foo) {
if (!this.bitmap$0) {
new Serializable(this){
public static final long serialVersionUID = 0L;
private final /* synthetic */ Foo $outer;
public final void apply(String x$1) {
this.$outer.Foo$$bar(x$1);
}
{
if ($outer == null) {
throw null;
}
this.$outer = $outer;
}
};
this.self = this.dummy(BoxedUnit.UNIT);
this.bitmap$0 = true;
}
return this.self;
}
}
public int self() {
return this.bitmap$0 ? this.self : this.self$lzycompute();
}
public int dummy(BoxedUnit u) {
return 1;
}
public void Foo$$bar(String x) {
String string = x;
if ("blah".equals(string)) {
Predef$.MODULE$.println((Object)new StringContext(
(Seq)Predef$.MODULE$.wrapRefArray((Object[])new String[]{"x = ", ""})
).s((Seq)Predef$.MODULE$.genericWrapArray((Object)new Object[]{x}))
);
BoxedUnit boxedUnit = BoxedUnit.UNIT;
return;
}
throw new MatchError((Object)string);
}

Related

Call Higher Order function of Kotlin from Java

Kotin Class
class LoginService{
fun getLoginData(loginData: String) {
request(LoginApi.create().getLoginData(loginData))}
}
fun changePassword(_:String){
request(LoginApi.create().changePassword(_)
}
class RequestData {
var retrofitCall: ((String) -> Unit)? = null
}
}
Java Class
class LoginModel{
private void loginData(){
RequestData data = new RequestData();
requestData.setRetrofitCall(username ->LoginService::getLoginData)
}
private void changePassword(){
RequestData data = new RequestData();
requestData.setRetrofitCall(username ->LoginService::changePassword)
}
}
requestData.setRetrofitCall(username ->LoginService::changePassword)
Why Higher order function :
Since i have to differentiate between each API call for calling a function from its feature hence trying to use.
How to call the above highlighted code?
Using Kotlin Functional Interfaces in Java is a little tricky and not very clean.
Your setRetrofitCall() would need to look something like this:
setRetrofitCall(new Function1<String, Unit>() {
#Override
public Unit invoke(String s) {
LoginService.getLoginData(s); //I'm pretty sure LoginService is supposed to be static?
return Unit.INSTANCE;
}
}
);
More short code with Lamda expression
setRetrofitCall(s -> {
LoginService.getLoginData(s);
return Unit.INSTANCE;
});
If you want to use a named class and don't want to refer to Kotlin's interfaces, you can use ::.
class CallbackHandler {
Unit invoke(String s) {
LoginService.getLoginData(s);
return Unit.INSTANCE;
}
}
CallbackHandler handler = new CallbackHandler();
setRetrofitCall(handler::invoke);
Otherwise for anonymous class you can use lambda expression per chakrapani's answer.
setRetrofitCall(s -> {
LoginService.getLoginData(s);
return Unit.INSTANCE;
});

Looking for JEXL Filter feature

I know, I can do several things in JEXL, but unable to find Filter feature in it, which is indeed very useful.
How can I do something like
var x=[{a:11,b=5},{a:1,b=15},{a:12,b=25},{a:4,b=35},{a:7,b=45}];
return x[.a>10].b; // Which filters to {a:11,b=5} & {a:12,b=25}
// & hence returns [5,25]
First of all your syntax is not valid JEXL. I assume you meant this:
var x = [{'a':11,'b':5}, {'a':1,'b':15}, {'a':12,'b':25}, {'a':4,'b':35}, {'a':7,'b':45}];
Since you can call any Java method on any object in a JEXL script, you have (at least theoretically) full access to the Java Stream API.
However, the Stream API isn't directly available from a raw array and we can't just call Arrays.stream(x); without some effort. The easiest way around this is to create a set instead:
var x = {{'a':11,'b':5}, {'a':1,'b':15}, {'a':12,'b':25}, {'a':4,'b':35}, {'a':7,'b':45}};
Now we can simply call stream() and work from there:
x.stream();
What we want now is something like this:
x.stream().filter(function(m){m['a']>10});
Unfortunately the method resolver in JEXL will not be able to correctly resolve Stream.filter(Predicate) with a JEXL function, as it doesn't know how to turn a JEXL function into a Predicate. A JEXL function is of type org.apache.commons.jexl3.internal.Closure.
Thus the very least you need to do is to provide your own Predicate implementation in Java and then create a new instance in your script:
public class MyCustomFilterPredicate implements Predicate<HashMap<String, Integer>> {
#Override
public boolean test(final HashMap<String, Integer> m)
{
return m.get("a") > 10;
}
}
You can then create a new instance in your JEXL script:
var filterPredicate = new('my.custom.filter.predicate.MyCustomFilterPredicate');
The same goes for Stream.map(Function):
public class MyCustomMapFunction implements Function<HashMap<String, Integer>, Integer> {
#Override
public Integer apply(final HashMap<String, Integer> m)
{
return m.get("b");
}
}
And again create a new instance in your script:
var mapFunction = new('my.custom.map.function.MyCustomMapFunction');
Your entire script will then look like this:
var x = {{'a':11,'b':5}, {'a':1,'b':15}, {'a':12,'b':25}, {'a':4,'b':35}, {'a':7,'b':45}};
var filterPredicate = new('my.custom.filter.predicate.MyCustomFilterPredicate');
var mapFunction = new('my.custom.map.function.MyCustomMapFunction');
return x.stream().filter(filterPredicate).map(mapFunction).toArray();
Of course you might have noticed that the reusability of your predicate and function implementations are rather limited. This is why I'd recommend creating implementations that wrap a JEXL Closure:
public class MyCustomFilterPredicate implements Predicate<Object> {
private final Closure closure;
public MyCustomFilterPredicate(final Closure closure) {
this.closure = closure;
}
#Override
public boolean test(final Object o)
{
return (boolean) closure.execute(JexlEngine.getThreadContext(), o);
}
}
public class MyCustomMapFunction implements Function<Object, Object> {
private final Closure closure;
public MyCustomMapFunction(final Closure closure) {
this.closure = closure;
}
#Override
public Object apply(final Object o)
{
return closure.execute(JexlEngine.getThreadContext(), o);
}
}
Now you can change your script as follows and reuse these Java classes in various ways:
var x = {{'a':11,'b':5}, {'a':1,'b':15}, {'a':12,'b':25}, {'a':4,'b':35}, {'a':7,'b':45}};
var filterPredicate = new('my.custom.filter.predicate.MyCustomFilterPredicate', function(m){m['a']>10});
var mapFunction = new('my.custom.map.function.MyCustomMapFunction', function(m){m['b']});
return x.stream().filter(filterPredicate).map(mapFunction).toArray();

How to implement method with generics in C#

I have such code in Java. How to make similar solution in C#.
Especially i'm interested how to implement the first and the last rows?
This code goes through specified package (that contains forms for Android and iOS) and returns Android form instance or iOS depending on getTargetPlatform()
public static <T extends Helpers> T getPage(Class pageInterface) throws Exception {
Set<Class<?>> allClasses = new Reflections("forms", new SubTypesScanner(false)).getSubTypesOf(Object.class);
for (Class pageClass : allClasses) {
if (pageInterface.isAssignableFrom(pageClass) && pageClass.getName().contains(String.format(".%1$s.", getTargetPlatform()))) {
return (T) pageClass.newInstance();
}
}
return (T) pageInterface.newInstance();}
Depending on your usecase, you could scan assemblies for your type. Example:
public static T GetPage<T>(Type pageInterface) where T : Helpers
{
// maybe you need to scan different assemblies, depending on your usecase
var allTypes = Assembly.GetExecutingAssembly().GetTypes();
foreach (var pageType in allTypes)
{
if (pageInterface.IsAssignableFrom(pageType) && pageType.Name.Contains(String.Format(".%1$s.", GetTargetPlatform())))
{
return (T)Activator.CreateInstance(pageType);
}
}
return (T)Activator.CreateInstance(pageInterface);
}
first row is like below:
public static T getPage<T>(Class pageInterface) where T: Helpers
and last row is just the same:
return (T) pageInterface.newInstance();

How to convert one enum to another enum in java?

I have;
public enum Detailed {
PASSED, INPROCESS, ERROR1, ERROR2, ERROR3;
}
and need to convert it to the following;
public enum Simple {
DONE, RUNNING, ERROR;
}
So first PASSED->DONE and INPROCESS->RUNNING, but all errors should be: ERROR. Obviously it is possible to write cases for all values, but there may be a better solution?
Personally I would just create a Map<Detailed, Simple> and do it explicitly - or even use a switch statement, potentially.
Another alternative would be to pass the mapping into the constructor - you could only do it one way round, of course:
public enum Detailed {
PASSED(Simple.DONE),
INPROCESS(Simple.RUNNING),
ERROR1(Simple.ERROR),
ERROR2(Simple.ERROR),
ERROR3(Simple.ERROR);
private final Simple simple;
private Detailed(Simple simple) {
this.simple = simple;
}
public Simple toSimple() {
return simple;
}
}
(I find this simpler than Ted's approach of using polymorphism, as we're not really trying to provide different behaviour - just a different simple mapping.)
While you could potentially do something cunning with the ordinal value, it would be much less obvious, and take more code - I don't think there'd be any benefit.
One way is to define a method asSimple() in your Detailed enum:
public enum Detailed {
PASSED {
#Override
Simple asSimple() {
return DONE;
}
},
INPROCESS {
#Override
Simple asSimple() {
return RUNNING;
}
},
ERROR1,
ERROR2,
ERROR3;
public Simple asSimple() {
return Simple.ERROR; // default mapping
}
}
You can then simply call the method when you want to do the mapping:
Detailed code = . . .
Simple simpleCode = code.asSimple();
It has the advantage of putting the knowledge of the mapping with the Detailed enum (where perhaps it belongs). It has the disadvantage of having knowledge of Simple mixed in with the code for Detailed. This may or may not be a bad thing, depending on your system architecture.
Use EnumMap
I decouple my external xml interface from my internal domain model by implementing a transformation service. This includes mapping enums from jaxb generated code to domain model enums.
Using a static EnumMap encapsulates the concern of transformation within the class responsible for transformation. Its cohesive.
#Service
public class XmlTransformer {
private static final Map<demo.xml.Sense, Constraint.Sense> xmlSenseToSense;
static {
xmlSenseToSense = new EnumMap<demo.xml.Sense, Constraint.Sense> (
demo.xml.Sense.class);
xmlSenseToSense.put(demo.xml.planningInterval.Sense.EQUALS,
Constraint.Sense.EQUALS);
xmlSenseToSense.put(demo.xml.planningInterval.Sense.GREATER_THAN_OR_EQUALS,
Constraint.Sense.GREATER_THAN_OR_EQUALS);
xmlSenseToSense.put(demo.xml.planningInterval.Sense.LESS_THAN_OR_EQUALS,
Constraint.Sense.LESS_THAN_OR_EQUALS);
}
...
}
Guava's Enums.getIfPresent() on Enum.name()
Our case was a particular specialization of this one. We do have two Enum: one we use in the application and another one we use in the core library. The core library is used by a handful of applications, by different teams. Each application views a subset of the whole functionality. The whole functionality is configured with the enums in order to switch on and off, throttle up or down, select strategies, etc.
So we ended up with:
one enum for the library, containing all the possible configurations, visible from the applications and also some library-specific
one enum for each application, containing the literals corresponding to what the application can see/touch in the library, and some application-specific
Then as we pass data down to the library, we adapt all data and also those configurations. We own all enums, so we can choose to call the same configuration with the same literal in different enums.
Enum LibraryConfig {
FUNCTION_ONE,
FUNCTION_TWO,
FUNCTION_THREE,
FUNCTION_FOUR;
}
Enum Aplication1Config {
FUNCTION_ONE,
FUNCTION_TWO,
FUNCTION_THREE,
APPL1_FUNCTION_ONE,
APPL1_FUNCTION_TWO;
}
Enum Aplication2Config {
FUNCTION_ONE,
FUNCTION_TWO,
FUNCTION_FOUR;
APPL2_FUNCTION_ONE;
}
When we need to convert from one type to another (app --> lib or lib --> app) we use the getIfPresent() method from com.google.common.base.Enums in this way:
Aplication1Config config1App1 = FUNCTION_TWO;
LibraryConfig configLib = Enums.getIfPresent(LibraryConfig.class, config1App1.name()).orNull();
We check configLib for null value to see if there was successful conversion. This last step we use because of the APPX_FUNCTION_YYY, which are application-specific, and for the conversion on the direction lib --> app, not to pass configuration values library-specific (FUNCTION_FOUR in the example).
maven's dependency management:
Just in case anyone needs it:
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>20.0</version>
</dependency>
Home grown version:
You can make your own conversion using the Enum methods, but you have to take care of the exception to detect when the conversion did not succeed:
try {
Aplication1Config config1App1 = FUNCTION_TWO;
LibraryConfig configLib = LibraryConfig.valueOf(config1App1.name());
} catch (IllegalArgumentException iae) {
// if the conversion did not succeed
}
Ted's answer is very Javaly, but the expression
passed == PASSED ? DONE : ERROR
would do the job, too.
To me that sounds more like a conceptual problem than a programming problem. Why don't you just remove the "Simple" enum type and use the other one instead in all places in the program?
Just to make that more clear with another example: Would you really try to define an enum type for the work days in a week (Monday to Friday) and another enum for all days of a week (Monday to Sunday)?
Here is the simple enum mapper with test:
-- IMPLEMENTATION
-- ENUMS
public enum FirstEnum {
A(0), B(1);
private final int value;
private FirstEnum(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
public enum SecondEnum {
C(0), D(1);
private final int valueId;
private SecondEnum(int valueId) {
this.valueId = valueId;
}
public int getValueId() {
return valueId;
}
}
--MAPPER
import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang3.Validate;
import com.google.common.collect.Sets;
public class EnumPropertyMapping {
private final Map<?, ?> firstMap;
private final Map<?, ?> secondMap;
private final Class<?> firstType;
private final Class<?> secondType;
private EnumPropertyMapping(
Map<?, ?> firstMap, Map<?, ?> secondMap, Class<?> firstType, Class<?> secondType) {
this.firstMap = firstMap;
this.secondMap = secondMap;
this.firstType = firstType;
this.secondType = secondType;
}
public static Builder builder() {
return new Builder();
}
#SuppressWarnings("unchecked")
public <R> R getCorrespondingEnum(Object mappedEnum) {
Validate.notNull(mappedEnum, "Enum must not be NULL");
Validate.isInstanceOf(Enum.class, mappedEnum, "Parameter must be an Enum");
if (firstType.equals(mappedEnum.getClass())) {
return (R) firstMap.get(mappedEnum);
}
if (secondType.equals(mappedEnum.getClass())) {
return (R) secondMap.get(mappedEnum);
}
throw new IllegalArgumentException("Didn't found mapping for enum value: " + mappedEnum);
}
public static class Builder {
private final Map<Object, Object> firstEnumMap = new HashMap<>();
private final Map<Object, Object> secondEnumMap = new HashMap<>();
private Class<?> firstEnumType;
private Class<?> secondEnumType;
public <T extends Enum<T>> Builder addFirst(Class<T> enumType, String propertyName) {
firstEnumType = enumType;
initMap(firstEnumMap, enumType.getEnumConstants(), propertyName);
return this;
}
public <T extends Enum<T>> Builder addSecond(Class<T> enumType, String propertyName) {
secondEnumType = enumType;
initMap(secondEnumMap, enumType.getEnumConstants(), propertyName);
return this;
}
private void initMap(Map<Object, Object> enumMap, Object[] enumConstants, String propertyName) {
try {
for (Object constant : enumConstants) {
enumMap.put(PropertyUtils.getProperty(constant, propertyName), constant);
}
} catch (InvocationTargetException | NoSuchMethodException | IllegalAccessException ex) {
throw new IllegalStateException(ex);
}
}
public EnumPropertyMapping mapEnums() {
Validate.isTrue(firstEnumMap.size() == secondEnumMap.size());
Validate.isTrue(Sets.difference(firstEnumMap.keySet(), secondEnumMap.keySet()).isEmpty());
Map<Object, Object> mapA = new HashMap<>();
Map<Object, Object> mapB = new HashMap<>();
for (Map.Entry<Object, Object> obj : firstEnumMap.entrySet()) {
Object secondMapVal = secondEnumMap.get(obj.getKey());
mapA.put(obj.getValue(), secondMapVal);
mapB.put(secondMapVal, obj.getValue());
}
return new EnumPropertyMapping(mapA, mapB, firstEnumType, secondEnumType);
}
}
}
-- TEST
import org.junit.Test;
import com.bondarenko.common.utils.lang.enums.FirstEnum;
import com.bondarenko.common.utils.lang.enums.SecondEnum;
import static junit.framework.TestCase.assertEquals;
public class EnumPropertyMappingTest {
#Test
public void testGetMappedEnum() {
EnumPropertyMapping mapping = EnumPropertyMapping.builder()
.addSecond(SecondEnum.class, "valueId")
.addFirst(FirstEnum.class, "value")
.mapEnums();
assertEquals(SecondEnum.D, mapping.getCorrespondingEnum(FirstEnum.B));
assertEquals(FirstEnum.A, mapping.getCorrespondingEnum(SecondEnum.C));
}
}

Can I add new methods to the String class in Java?

I'd like to add a method AddDefaultNamespace() to the String class in Java so that I can type "myString".AddDefaultNamespace() instead of DEFAULTNAMESPACE + "myString", to obtain something like "MyDefaultNameSpace.myString". I don't want to add another derived class either (PrefixedString for example).
Maybe the approach is not good for you but I personally hate using +. But, anyway, is it possible to add new methods to the String class in Java?
Thanks and regards.
String is a final class which means it cannot be extended to work on your own implementation.
Well, actually everyone is being unimaginative. I needed to write my own version of startsWith method because I needed one that was case insensitive.
class MyString{
public String str;
public MyString(String str){
this.str = str;
}
// Your methods.
}
Then it's quite simple, you make your String as such:
MyString StringOne = new MyString("Stringy stuff");
and when you need to call a method in the String library, simple do so like this:
StringOne.str.equals("");
or something similar, and there you have it...extending of the String class.
As everyone else has noted, you are not allowed to extend String (due to final). However, if you are feeling really wild, you can modify String itself, place it in a jar, and prepend the bootclasspath with -Xbootclasspath/p:myString.jar to actually replace the built-in String class.
For reasons I won't go into, I've actually done this before. You might be interested to know that even though you can replace the class, the intrinsic importance of String in every facet of Java means that it is use throughout the startup of the JVM and some changes will simply break the JVM. Adding new methods or constructors seems to be no problem. Adding new fields is very dicey - in particular adding Objects or arrays seems to break things although adding primitive fields seems to work.
It is not possible, since String is a final class in Java.
You could use a helper method all the time you want to prefix something. If you don't like that you could look into Groovy or Scala, JRuby or JPython both are languages for the JVM compatible with Java and which allow such extensions.
YES!
Based on your requirements (add a different namespace to a String and not use a derived class) you could use project Lombok to do just that and use functionality on a String like so:
String i = "This is my String";
i.numberOfCapitalCharacters(); // = 2
Using Gradle and IntelliJ idea follow the steps below:
Download the lombok plugin from intelliJ plugins repository.
add lombok to dependencies in gradle like so: compileOnly 'org.projectlombok:lombok:1.16.20'
go to "Settings > Build > Compiler > Annotation Processors" and enable annotation processing
create a class with your extension functions and add a static method like this:
public class Extension {
public static String appendSize(String i){
return i + " " + i.length();
}
}
annotate the class where you want to use your method like this:
import lombok.experimental.ExtensionMethod;
#ExtensionMethod({Extension.class})
public class Main {
public static void main(String[] args) {
String i = "This is a String!";
System.out.println(i.appendSize());
}
}
Now you can use the method .appendSize() on any string in any class as long as you have annotated it and the produced result for the above example
This is a String!
would be:
This is a String! 17
The class declaration says it all pretty much,as you cannot inherit it becouse it's final.
You can ofcourse implement your own string-class, but that is probaby just a hassle.
public final class String
C# (.net 3.5) have the functionality to use extender metods but sadly java does not. There is some java extension called nice http://nice.sourceforge.net/ though that seems to add the same functionality to java.
Here is how you would write your example in the Nice language (an extension of
Java):
private String someMethod(String s)
{
return s.substring(0,1);
}
void main(String[] args)
{
String s1 = "hello";
String s2 = s1.someMethod();
System.out.println(s2);
}
You can find more about Nice at http://nice.sf.net
Not possible, and that's a good thing. A String is a String. It's behaviour is defined, deviating from it would be evil. Also, it's marked final, meaning you couldn't subclass it even if you wanted to.
As everybody else has said, no you can't subclass String because it's final. But might something like the following help?
public final class NamespaceUtil {
// private constructor cos this class only has a static method.
private NamespaceUtil() {}
public static String getDefaultNamespacedString(
final String afterDotString) {
return DEFAULT_NAMESPACE + "." + afterDotString;
}
}
or maybe:
public final class NamespacedStringFactory {
private final String namespace;
public NamespacedStringFactory(final String namespace) {
this.namespace = namespace;
}
public String getNamespacedString(final String afterDotString) {
return namespace + "." + afterDotString;
}
}
People searching with keywords "add method to built in class" might end up here. If you're looking to add method to a non final class such as HashMap, you can do something like this.
public class ObjectMap extends HashMap<String, Object> {
public Map<String, Object> map;
public ObjectMap(Map<String, Object> map){
this.map = map;
}
public int getInt(String K) {
return Integer.valueOf(map.get(K).toString());
}
public String getString(String K) {
return String.valueOf(map.get(K));
}
public boolean getBoolean(String K) {
return Boolean.valueOf(map.get(K).toString());
}
#SuppressWarnings("unchecked")
public List<String> getListOfStrings(String K) {
return (List<String>) map.get(K);
}
#SuppressWarnings("unchecked")
public List<Integer> getListOfIntegers(String K) {
return (List<Integer>) map.get(K);
}
#SuppressWarnings("unchecked")
public List<Map<String, String>> getListOfMapString(String K) {
return (List<Map<String, String>>) map.get(K);
}
#SuppressWarnings("unchecked")
public List<Map<String, Object>> getListOfMapObject(String K) {
return (List<Map<String, Object>>) map.get(K);
}
#SuppressWarnings("unchecked")
public Map<String, Object> getMapOfObjects(String K) {
return (Map<String, Object>) map.get(K);
}
#SuppressWarnings("unchecked")
public Map<String, String> getMapOfStrings(String K) {
return (Map<String, String>) map.get(K);
}
}
Now define a new Instance of this class as:
ObjectMap objectMap = new ObjectMap(new HashMap<String, Object>();
Now you can access all the method of the built-in Map class, and also the newly implemented methods.
objectMap.getInt("KEY");
EDIT:
In the above code, for accessing the built-in methods of map class, you'd have to use
objectMap.map.get("KEY");
Here's an even better solution:
public class ObjectMap extends HashMap<String, Object> {
public ObjectMap() {
}
public ObjectMap(Map<String, Object> map){
this.putAll(map);
}
public int getInt(String K) {
return Integer.valueOf(this.get(K).toString());
}
public String getString(String K) {
return String.valueOf(this.get(K));
}
public boolean getBoolean(String K) {
return Boolean.valueOf(this.get(K).toString());
}
#SuppressWarnings("unchecked")
public List<String> getListOfStrings(String K) {
return (List<String>) this.get(K);
}
#SuppressWarnings("unchecked")
public List<Integer> getListOfIntegers(String K) {
return (List<Integer>) this.get(K);
}
#SuppressWarnings("unchecked")
public List<Map<String, String>> getListOfMapString(String K) {
return (List<Map<String, String>>) this.get(K);
}
#SuppressWarnings("unchecked")
public List<Map<String, Object>> getListOfMapObject(String K) {
return (List<Map<String, Object>>) this.get(K);
}
#SuppressWarnings("unchecked")
public Map<String, Object> getMapOfObjects(String K) {
return (Map<String, Object>) this.get(K);
}
#SuppressWarnings("unchecked")
public Map<String, String> getMapOfStrings(String K) {
return (Map<String, String>) this.get(K);
}
#SuppressWarnings("unchecked")
public boolean getBooleanForInt(String K) {
return Integer.valueOf(this.get(K).toString()) == 1 ? true : false;
}
}
Now you don't have to call
objectMap.map.get("KEY");
simply call
objectMap.get("KEY");
Better use StringBuilder, which has method append() and does the job you want. The String class is final and can not be extended.
No You Cannot Modify String Class in java. Because It's final class. and every method present in final class by default will be final.
The absolutely most important reason that String is immutable or final is that it is used by the class loading mechanism, and thus have profound and fundamental security aspects.
Had String been mutable or not final, a request to load "java.io.Writer" could have been changed to load "mil.vogoon.DiskErasingWriter"
All is said by the other contributors before. You can not extend String directly because it is final.
If you would use Scala, you can use implicit conversions like this:
object Snippet {
class MyString(s:String) {
def addDefaultNamespace = println("AddDefaultNamespace called")
}
implicit def wrapIt(s:String) = new MyString(s)
/** test driver */
def main(args:Array[String]):Unit = {
"any java.io.String".addDefaultNamespace // !!! THAT is IT! OR?
}
The Java String class is a final, making it immutable. This is for efficiency reasons and that it would be extremely difficult to logically extend without error; the implementers have therefore chosen to make it a final class meaning it cannot be extended with inheritance.
The functionality you wish your class to support is not properly part of the regular responsibilities of a String as per the single responsibility principle, a namespace it is a different abstraction, it is more specialised. You should therefore define a new class, which includes String a member and supports the methods you need to provide the namespace management you require.
Do not be afraid to add abstractions (classes) these are the essence of good OO design.
Try using a class responsibility collaboration (CRC) card to clarify the abstraction you need.
You can do this easily with Kotlin. You can run both the kotlin code from within the java and the java code from the kotlin.
Difficult jobs that you can do with Java can be done more easily with Kotlin. I recommend every java developer to learn kotlin.
Referance: https://kotlinlang.org/docs/java-to-kotlin-interop.html
Example:
Kotlin StringUtil.kt File
#file:JvmName("StringUtil")
package com.example
fun main() {
val x: String = "xxx"
println(x.customMethod())
}
fun String.customMethod(): String = this + " ZZZZ"
Java Code:
package com.example;
public class AppStringCustomMethod {
public static void main(String[] args) {
String kotlinResponse = StringUtil.customMethod("ffff");
System.out.println(kotlinResponse);
}
}
output:
ffff ZZZZ
You can create your own version of String class and add a method :-)
Actually , you can modify the String class . If you edit the String.java file located in src.zip , and then rebuild the rt.jar , the String class will have more methods added by you . The downside is that that code will only work on your computer , or if you provide your String.class , and place it in the classpath before the default one .

Categories

Resources