package lambda;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class Example {
static int var1 = 10;
public static void example1() {
System.out.println("Example1");
Button button = new Button("Click me!");
// запиÑÑ Ð² ÑÑиле анонимного клаÑÑа Ñ ÑÑнкÑионалÑнÑм инÑеÑÑейÑом
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
JOptionPane.showMessageDialog(null, "Pressed");
}
});
int i = 10; // внеÑние пеÑеменнÑй заÑ
ваÑÑваÑÑÑÑ Ð»Ñмбдой
// огÑаниÑÐµÐ½Ð¸Ñ Ð² Java: пеÑÐµÐ¼ÐµÐ½Ð½Ð°Ñ Ð´Ð¾Ð»Ð¶Ð½Ð° бÑÑÑ immutable (final)
var1 = 20; // менÑеÑÑÑ ÑолÑко знаÑение по ÑÑÑлке, но не Ñама пеÑеменнаÑ
AtomicInteger atomicInteger = new AtomicInteger(10);
atomicInteger.set(20); // внÑÑÑеннее ÑвойÑÑво можно менÑÑÑ
button.addActionListener(e -> JOptionPane.showMessageDialog(null, "Pressed" + i));
button.addActionListener(e -> JOptionPane.showMessageDialog(null, "Pressed" + var1));
button.addActionListener(e -> JOptionPane.showMessageDialog(null, "Pressed" + atomicInteger.toString()));
}
@FunctionalInterface
interface FuncInterface {
R apply(T1 arg1, T2 arg2);
}
@FunctionalInterface
interface FuncInterface1 {
R apply(T1 arg1);
}
public static void example2()
{
System.out.println("Example2");
FuncInterface function = (arg1, arg2) -> arg1 + arg2;
System.out.println(function.apply(1, 5));
}
public static void example3()
{
System.out.println("Example3");
// ÑеализаÑÐ¸Ñ ÑÑнкÑионалÑного инÑеÑÑеÑа Ð¼Ð¾Ð¶ÐµÑ Ð±ÑÑÑ Ð·Ð°Ð¼ÐµÐ½ÐµÐ½Ð° ÑÑÑлкой на ÑÑнкÑÐ¸Ñ Ñ Ñакой же ÑигнаÑÑÑой меÑода
FuncInterface myFunction = Integer::sum;
System.out.println(myFunction.apply(3, 5));
FuncInterface1 myFunction1 = Integer::reverse;
System.out.println(myFunction1.apply(1));
}
public static void example4()
{
System.out.println("Example4");
ArrayList clients = new ArrayList<>();
clients.add(new Client("Igor", 5, true));
clients.add(new Client("Vasya", 10, true));
clients.add(new Client("Petya", 20, false));
clients.add(new Client("Masha", 30, true));
clients.add(new Client("Liza", 40, false));
// ÑменÑÑение баланÑа в Ñикле
for (Client client: clients) {
if (client.getActive()) {
client.setSum(client.getSum() - 1);
System.out.println(client.getSum());
}
}
System.out.println();
// ÑменÑÑение баланÑа Ñ Ð¿Ð¾Ð¼Ð¾ÑÑÑ Ð»ÑмбдÑ
clients.forEach(client -> {
client.setSum(client.getSum() - 1);
System.out.println(client.getSum());
});
System.out.println();
// подÑÑÐµÑ ÑÑÐ¼Ð¼Ñ ÑлеменÑов Ñ Ð¾Ð´Ð½Ð¾Ð²Ñеменной ÑилÑÑÑаÑией
int balance = clients.stream()
.filter(c -> c.getSum() > 7) // Ñ Ð±Ð°Ð»Ð°Ð½Ñом болÑÑе 7
.filter(Client::getActive) // ÑолÑко акÑивнÑе
.reduce(0, ((integer, client) -> integer + client.getSum()), Integer::sum);
System.out.println("balance = " + balance);
}
public static void example5()
{
Client cl = new Client("Igor", 5, true);
// добавлÑем пÑивеÑÑÑвие к имени, полÑзÑÑÑÑ ÑÑÑлками на меÑодÑ
addHello(cl::getName, cl::setName);
System.out.println(cl.getName());
}
/**
* ÐеÑедаÑÑÑÑ ÑÑÑлки на меÑодÑ
* @param sup ÑÑÑлка на меÑод полÑÑÐµÐ½Ð¸Ñ Ð¸Ð¼ÐµÐ½Ð¸ клиенÑа
* @param cons ÑÑÑлка на меÑод пÑиÑÐ²Ð¾ÐµÐ½Ð¸Ñ Ð¸Ð¼ÐµÐ½Ð¸ клиенÑÑ
*/
public static void addHello(Supplier sup, Consumer cons)
{
cons.accept(String.format("Hello, %s !", sup.get()));
}
/**
* ÐÑовеÑÑем, ÑÑо обÑÐµÐºÑ Ð½Ðµ null и ÑолÑко поÑле ÑÑого вÑводим его имÑ
* Таким обÑазом избегаем NullPointerException
*/
public static void example6()
{
Client client1 = null;
Client client2 = new Client("Igor", 5, true);
chechForNull(client1, c -> {
System.out.println(client1.getName());
});
chechForNull(client2, c -> {
System.out.println(client2.getName());
});
}
public static void chechForNull(T param, Consumer cons)
{
if (param != null) {
cons.accept(param);
}
}
public static void example7()
{
Client client1 = new Client("Igor", 5, true);
ManagerClient managerClient = new ManagerClient(client1);
managerClient.change(c -> c.setName("New value"));
}
static class ManagerClient
{
private final Client client;
public ManagerClient(Client client) {
this.client = client;
}
public void change(Consumer consumer)
{
System.out.println("ÐÑзов оÑновного меÑода");
consumer.accept(client);
updateLog();
}
public void updateLog()
{
System.out.println("ÐÑзов дополниÑелÑного меÑода");
}
}
}