How is spark's scala code exposed as a Java API? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
As I know Apache Spark is written in Scala. But its functionality is also exposed as a Java API[1], which in turn can be used in Java programs.
How is this done? Can someone explain me using an example.
In other words if I write a Scala program, and I want to expose it as a java API, what steps should be taken?
[1]http://spark.apache.org/docs/latest/api/java/

It will surely depend on the type of api you are exposing, but in general
Simple methods can be called as is: class A { def doSomething(s: String) } in class A can be called just like a regular java method new A().doSomehing("hello");
Default parameters in methods will not work. You will always have to call the method with the whole parameter list.
traits with behaviour can't be implemented, but if you have common combinations you could create an abstract class and then you can extend that in java. Not sure if this will be solved with default methods for java8 in scala2.12
For object functions you need to call using the escaped path. e.g. object Container { val answer = 42 } you get the constant value from java like int answer = Container$.MODULE$.answer;.
As I said first, it mostly depends on the api you want to expose from scala to java. If you have more specific cases I can edit them in this answer.

Related

When to use public modifier for a class field in OOP? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 12 months ago.
Improve this question
When programming in OO languages like C# or Java, is there a good situation where declaring a public field inside a class is actually valid (I myself always use a property for not making the user of the class depend on the data and to support data protection)?
Otherwise, it feels weird that C# for example allows you to do so.
According to the C# coding conventions public field should be used sparingly:
// A public field, these should be used sparingly
public bool IsValid;
Why? I think because of:
can be edited by any other user of class
if you want to add some logic to field, then you need to create property instead of field. By doing this, you will break a contract of class
it is not possible to override variable
However, there is a case when you need to have field as #VGR said:
public const string foo = "";

Convert object from one class to another in java [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 2 years ago.
Improve this question
I have 2 custom classes A and B.
Now I have an object of A let say 'a' and have to convert it to B class.
Now I have 2 approach
First is I write a transform Util which has a static method for conversion.
The second approach is to write that logic in class A with a method convertToB()
Which one is more accurate. Please suggest.
In short, it depends on the relationship between A and B.
If B extends A the conversion is done within the language when assigning an A object to a B object.
Otherwise, if there is no inheritance at play, it would be advised to either create a util class to do the conversion for the case of future changes, or have a constructor of B that has an A object as a parameter.
This is a matter of preference and a question perhaps better suited for the software engineering/design counterpart to Stack Overflow. That said, I would use the first option. If you ever add more classes and need to convert between them, you will bloat your classes with all the conversion methods. If you have one or more classes with the conversion methods, the code is cleaner.
Separation of Concerns - util class is the best approach

which feature in java 1.7 paved the way for lambda expression in 1.8? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I want to understand the feature in java 1.7 which paved the way for lambda expression in Java 1.8. Anonymous function is legacy feature of Java , it exist even before Java 1.7.
Comparator<String> c = new Comparator<String>() {
int compare(String s, String s2) { ... }
};
They are called anonymous classes, not anonymous functions. To find out more about them, your "first stop" should be the Oracle Java Tutorial:
Anonymous Classes
Anonymous classes have existed since Java 1.1. However, it is a stretch to call them "legacy". There are significant differences between anonymous classes and lambdas. For example, an anonymous class may implement multiple methods, and may extend an existing class. By contrast, the innate functionality of a lambda is limited to the code in the lambda itself.

How translate to Swift an anonymous class of Java? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm new with Swift 2, I never had developed in Apple. Objective C has seemed ugly and you have to write a lot of code, so Swift liked because the syntax is similar to Java and C and here my question:
In Java you can define a class of this way:
new Thread(){
public run(){
// anything
}
}.start();
Then, Swift can do this? And how?
Thank and greetings
Solution
let myThread=NSThread(target: self, selector: "backgroundWork", object: nil)
myThread.start()
}
func backgroundWork(){
for (self.counter=0;self.counter<10;self.counter++){
//self.myLabel.text = String(self.counter) // Not update UI
self.performSelectorOnMainThread( Selector("updateLabel"), withObject: nil, waitUntilDone: false )
sleep(3)
}
}
func updateLabel(){
self.myLabel.text = String(self.counter)
}
Mostly this syntax (inline definition of anonymous classes) exists because Java doesn't allow the concept of closures or lambda functions, so if you want to pass a function to be invoked you have to pass an instance of a class with the function then declared inline.
In contrast, Swift, like most modern languages has a specific closure syntax which allows you to directly pass executable blocks to another routine, essentially it allows you to treat functions as first class language entities.
So, the bottom line is that no, Swift doesn't allow the construct you've asked about, but it does provide equivalent functionality for the dominant use case. The code most analogous to your example would be:
dispatch_async(...) {
Code to be executed asynchronously here
}
Which is really just syntactic sugar for:
dispatch_async(..., {
Your code here
})
Since the anonymous object is being created only as a holder for a single object, there's really no need for the object, or indeed, the class, and hence the syntax. The only time the Java syntax has a slight advantage is if the callee needs to maintain multiple related callbacks.

Regarding a Class Vs Enums [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
I was having a query , I have declared a class in which all the methods are static and it is following the utility design pattern that is it is acting like helper class Now can I replcae that class with correspond to enum also , Can I have enum having all the staic methods inside it, if Yes then what other advantages it offers ..!!
The problem with static method is: they can't be mocked for testing. At least not easily.
Putting the methods in an Enum with a single instance gets you a little closer. I'm not sure if enums can be mocked with the standard libraries, you certainly can't without using reflection.
But if you put your methods in an interface implemented by the enum, and everybody else just using the interface, accepting an instance of that interface via constructor (or setter if you have to) you can mock it as easily as you want.
Yes, you can use an enum as a utility class. There aren't many advantages to it, however: it boils down to the private constructor, which prevents uncontrolled instantiation. I would prefer sticking to the ordinary class with a private constructor since there's an expectation for an enum to be used for an enumerated type and not as a utility class. If you used enum for a singleton, that would give it only a slight bit more sense.

Categories

Resources