<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: omoluabidotcom</title>
    <description>The latest articles on DEV Community by omoluabidotcom (@omoluabidotcom).</description>
    <link>https://dev.to/omoluabidotcom</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F913665%2F47552e25-c8c7-4d68-8faa-3dd067938825.jpeg</url>
      <title>DEV Community: omoluabidotcom</title>
      <link>https://dev.to/omoluabidotcom</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/omoluabidotcom"/>
    <language>en</language>
    <item>
      <title>Rate-limiting API Endpoint using Bucket4j in Spring</title>
      <dc:creator>omoluabidotcom</dc:creator>
      <pubDate>Sun, 31 Dec 2023 21:49:26 +0000</pubDate>
      <link>https://dev.to/omoluabidotcom/rate-limiting-api-endpoint-using-bucket4j-in-spring-1hl7</link>
      <guid>https://dev.to/omoluabidotcom/rate-limiting-api-endpoint-using-bucket4j-in-spring-1hl7</guid>
      <description>&lt;p&gt;&lt;strong&gt;Overview&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In this article, you will learn how to implement a rate limit. Our focus will be to implement a rate limit for an endpoint using the Bucket4j library.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rate-Limiting API&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;There are different reasons to apply a rate limit to an API endpoint. One of many reasons would be to implement a rate limit based on the subscription plan on a system; another is that the request to login endpoint should be controlled from a unique request source, thereby regulating the number of requests made within a few minutes, as this can allow individuals with bad intent to carry out brute force attacks on your server, thereby leading to a server crash and making your application unavailable to users.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Bucket4J Library&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Bucket4j is a Java rate-limiting library that is mainly based on the token-bucket algorithm. The token bucket algorithm enables a network to allow or deny requests based on current traffic. Each bucket holds a certain number of tokens that represent network requests (e.g., attempting to log into an account or sending a message). Whenever a user sends a request, another token gets added to the bucket.&lt;/p&gt;

&lt;p&gt;Since the bucket has a set limit on how many tokens it can hold, the algorithm stops operations if users make too many requests in a short time. The network drops all new requests until a "bucket refill" resets the number of allowed tokens.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Using Bucket4j Library to Limit Endpoint&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Using the Bucket4j library is very simple, though it might get complex with more needed control. First, we need to setup a Spring application using the Spring initializr. Add the Spring web dependency. One additional dependency is the bucket4j library. Here is snippet to add to pom&lt;/p&gt;

&lt;p&gt;&lt;br&gt;
com.bucket4j&lt;br&gt;
bucket4j-core&lt;br&gt;
8.1.0&lt;br&gt;
&lt;/p&gt;

&lt;p&gt;Below are a few lines of code that just implemented rate limiting on the home endpoint. This is how simple it can be.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--NdehIKAj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gx3q0sejw337b3kd4uif.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--NdehIKAj--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_800/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gx3q0sejw337b3kd4uif.jpg" alt="Image description" width="800" height="420"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Code Snippet&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@RestController
@RequestMapping
public class RateLimiting {
Bucket bucket;
@GetMapping("/house")
public void initializeBucket() {
Refill refill = Refill.intervally(3, Duration.ofMinutes(1));
Bandwidth limit = Bandwidth.classic(3, refill);
bucket = Bucket.builder()
.addLimit(limit)
.build();
}
@GetMapping("/home")
public ResponseEntity&amp;lt;?&amp;gt; login() {
if(bucket.tryConsume(1)) {
System.out.println("Success");
return ResponseEntity.ok("Successful");
} else {
System.out.println("Too many requests");
return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).build();
}
}
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is all from me. Happy Holidays to you and your loved ones! Connect with me on &lt;a href="https://www.linkedin.com/in/yahaya-yusuf/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Credits&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.baeldung.com/spring-bucket4j"&gt;https://www.baeldung.com/spring-bucket4j&lt;/a&gt;&lt;/p&gt;

</description>
      <category>springboot</category>
      <category>apisecurity</category>
      <category>java</category>
      <category>security</category>
    </item>
    <item>
      <title>Part 2: Testing the Paystack Integration Using Postman</title>
      <dc:creator>omoluabidotcom</dc:creator>
      <pubDate>Fri, 31 Mar 2023 05:44:25 +0000</pubDate>
      <link>https://dev.to/omoluabidotcom/part-2-testing-the-paystack-integration-using-postman-2215</link>
      <guid>https://dev.to/omoluabidotcom/part-2-testing-the-paystack-integration-using-postman-2215</guid>
      <description>&lt;p&gt;This is a follow-up article to Part 1, which guided Paystack payment gateway integration. It is going to be a very short and straightforward guide. We are going to be using Postman to test our endpoint.&lt;/p&gt;

&lt;p&gt;This article will definitely not make sense if you haven't read Part 1, so please visit &lt;a href="https://dev.to/omoluabidotcom/integrate-paystack-payment-gateway-using-spring-boot-5fn2"&gt;here&lt;/a&gt; to read and come back here.&lt;br&gt;
We have three endpoints to make requests to accept payment.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CreatePlan Endpoint&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Starting with the first endpoint, "CreatePlan endpoint," from PaystackController POJO, we see that the Createplan methods require us to pass in a request body three parameters, as in the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu8sos9pb1fixofav47mh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fu8sos9pb1fixofav47mh.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The URL will be according to your configuration, and the request is a POST request. After hitting the endpoint, if everything goes well, the response will be as shown in the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8dn1bxbh2wzoeh33398e.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8dn1bxbh2wzoeh33398e.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;InitializePayment Endpoint&lt;/strong&gt;&lt;br&gt;
InitializePayment endpoint. From the name, it initiates the payment. This endpoint accepts a POST request, and its request body needs five JSON parameters as shown in the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38jdv6sih5etetyuhnts.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F38jdv6sih5etetyuhnts.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The initializepayment endpoint URL should be similar to the createplan just the last part makes them different. The amount here in the JSON body should be the same as the amount used in createplan endpoint, and the value of the plan here in initializepayment endpoint is gotten from the response in the createplan endpoint, in particular the plan_code value.&lt;br&gt;
Make a request to initializepayment endpoint if everything goes well, your response should be as in the image below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjebhnz3sydxh99dh5nad.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fjebhnz3sydxh99dh5nad.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The authorization_url in the response is where you redirect to. Since we are testing on the backend, copy the URL and paste it into a web browser, Paystack checkout page appears, and the user makes a payment, as shown in the pictures below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Checkout Page&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmfr83temaz4u7xq53mz0.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmfr83temaz4u7xq53mz0.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Payment Succesful&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fder32llxv0diio5ahsvd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fder32llxv0diio5ahsvd.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;VerifyPayment Endpoint&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Paystack makes available means to verify if payment has been made. The verifypayment endpoint accepts GET requests.&lt;br&gt;
Verifypayment endpoint needs three path variables to be passed in the URL to make a valid request as it relates to our implementation.&lt;br&gt;
The three path variables to be added are reference, plan, and id respectively.&lt;br&gt;
The reference is gotten from the initializepayment response body, the plan is the plan the user is paying for and the id is the user id.&lt;br&gt;
If everything goes as expected, the response would be like the picture below.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ee122g1kmx25qh3ta99.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F1ee122g1kmx25qh3ta99.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That is all for now, Thank you for your time. If you have questions, please drop them in the comment section. Have a great day.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Credit&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://paystack.com/docs/" rel="noopener noreferrer"&gt;Paystack Documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>paymentgateway</category>
      <category>paystack</category>
      <category>java</category>
      <category>springboot</category>
    </item>
    <item>
      <title>Integrate Paystack Payment Gateway using Spring Boot</title>
      <dc:creator>omoluabidotcom</dc:creator>
      <pubDate>Thu, 23 Mar 2023 17:23:36 +0000</pubDate>
      <link>https://dev.to/omoluabidotcom/integrate-paystack-payment-gateway-using-spring-boot-5fn2</link>
      <guid>https://dev.to/omoluabidotcom/integrate-paystack-payment-gateway-using-spring-boot-5fn2</guid>
      <description>&lt;p&gt;Instead of having to create the wheel from scratch, which is a lot of work, using third-party APIs to collect payment makes accepting payment from consumers simple.&lt;/p&gt;

&lt;p&gt;In this article, we will mostly be focusing on Paystack. I'll walk you through the process of receiving payment using the Paystack payment gateway.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisites&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Basic Spring boot&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Basic of Rest API&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Instead of creating test cases or handling exceptions, we will concentrate mostly on using the Paystack API to collect payments.&lt;/p&gt;

&lt;p&gt;Simply create an account on the Paystack &lt;a href="https://paystack.com/" rel="noopener noreferrer"&gt;website&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Setup a Spring Boot application with the following dependencies&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Spring Web&lt;/li&gt;
&lt;li&gt;Spring Data JPA&lt;/li&gt;
&lt;li&gt;MySQL Driver&lt;/li&gt;
&lt;li&gt;HTTPClient&lt;/li&gt;
&lt;li&gt;Hibernate Validation dependency&lt;/li&gt;
&lt;li&gt;Lombok&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Create the following packages in your project as seen below&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffjie1pqk48nq57kq2tzq.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffjie1pqk48nq57kq2tzq.jpg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the constants, the package creates a POJO APIConstants to add all static fields, as in below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;public class APIConstants {

public static final integer STATUS_CODE_OK = 200;
public static final integer STATUS_CODE_CREATED = 201;
    public static final String PAYSTACK_INIT = "https://api.paystack.co/plan";
    public static final String PAYSTACK_INITIALIZE_PAY = "https://api.paystack.co/transaction/initialize";
    public static final String PAYSTACK_VERIFY = "https://api.paystack.co/transaction/verify/";
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We need a POJO to map to create a table to store user information and payment information for reference because we are using Object Relational Mapping.&lt;/p&gt;

&lt;p&gt;In the model package, create a domain package. As seen in the code below, we have a POJO to map AppUser and PaymentPaystack.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AppUser&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name= "appuser")
public class AppUser {

    @Id
    @Column(name = "id", nullable = false)
    private Long id;

    @Column(name = "username", nullable = false)
    private String usernme;

    @Column(name = "name", nullable = false)
    private String name;

    @Column(name = "address", nullable = false)
    private String address;

    @CreationTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_on", updatable = false, nullable = false)
    private Date createdOn;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;PaymentPaystack&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name= "paystack_payment")
public class PaymentPaystack {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
    @JoinColumn(name = "user_id")
    private AppUser user;

    @Column(name = "reference")
    private String reference;

    @Column(name = "amount")
    private BigDecimal amount;

    @Column(name = "gateway_response")
    private String gatewayResponse;

    @Column(name = "paid_at")
    private String paidAt;

    @Column(name = "created_at")
    private String createdAt;

    @Column(name = "channel")
    private String channel;

    @Column(name = "currency")
    private String currency;

    @Column(name = "ip_address")
    private String ipAddress;

    @Column(name = "pricing_plan_type", nullable = false)
    @Enumerated(EnumType.STRING)
private pricing plan type PlanType = PricingPlanType BASIC;

    @CreationTimestamp
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "created_on", updatable = false, nullable = false)
    private Date createdOn;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Still within the model package create dto package for the data access layers POJOs we will be using.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CreatePlanDto&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CreatePlanDto {

    @NotNull(message = "Plan name cannot be null")
    @JsonProperty("name")
    private String name;

    @NotNull(message = "Interval cannot be null")
    @JsonProperty("interval")
    private String interval;

    @NotNull(message = "Amount cannot be null")
    @JsonProperty("amount")
    @Digits(integer = 6, fraction = 2)
    private Integer amount;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;IntializePaymentDto&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Getter
@Setter
@AllArgsConstructor@NoArgsConstructor
@Builder
public class InitializePaymentDto {

    @NotNull(message = "Amount cannot be null")
    @JsonProperty("amount")
    private String amount;

    @NotNull(message = "Email cannot be null")
    @JsonProperty("email")
    private String email;

    @NotNull(message = "Currency cannot be null")
    @JsonProperty("currency")
    private String currency;

    @NotNull(message = "Plan cannot be null")
    @JsonProperty("plan")
    private String plan;

    @NotNull(message = "Channels cannot be null")
    @JsonProperty("channels")
    private String[] channels;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;PaymentVerificationDto&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class PaymentVerificationDto {

    @JsonProperty("member_id")
    private AppUser user;

    @JsonProperty("reference")
    private String reference;

    @JsonProperty("amount")
    private BigDecimal amount;

    @JsonProperty("gateway_response")
    private String gatewayResponse;

    @JsonProperty("paid_at")
    private String paidAt;

    @JsonProperty("created_at")
    private String createdAt;

    @JsonProperty("channel")
    private String channel;

    @JsonProperty("currency")
    private String currency;

    @JsonProperty("ip_address")
    private String ipAddress;

    @JsonProperty("pricing_plan_type")
    private String pricingPlanType;

    @JsonProperty("created_on")
    private Date createdOn = new Date();
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;An enums package for pricing plan type enum in the model package as in below.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;PricingPlanType&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Getter
public enum PricingPlanType {

    BASIC("Basic"),
    STANDARD("Standard"),
    PREMIUM("Premium");

    private final String value;
    PricingPlanType(String value) {
        this.value = value;
    }

}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Response package sits inside the model package, and the POJOs under the response package are mapped to API responses.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;CreatePlanResponse&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class CreatePlanResponse {

    private Boolean status;
    private String message;
    private Data data;

    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Data {

        @JsonProperty("name")
        private String name;

        @JsonProperty("amount")
        private String amount;

        @JsonProperty("interval")
        private String interval;

        @JsonProperty("integration")
        private String integration;

        @JsonProperty("plan_code")
        private String planCode;

        @JsonProperty("send_invoices")
        private String sendInvoices;

        @JsonProperty("send_sms")
        private String sendSms;

        @JsonProperty("currency")
        private String currency;

        @JsonProperty("id")
        private String id;

        @JsonProperty("createdAt")
        private String createdAt;

        @JsonProperty("updatedAt")
        private String updatedAt;

    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;InitializePaymentResponse&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class InitializePaymentResponse {

    @JsonProperty("status")
    private Boolean status;

    @JsonProperty("message")
    private String message;

    @JsonProperty("data")
    private Data data;

    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonIgnoreProperties(ignoreUnknown = true)
    public class Data{

        @JsonProperty("authorization_url")
        private String authorizationUrl;

        @JsonProperty("access_code")
        private String accessCode;

        @JsonProperty("reference")
        private String reference;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;PaymentVerificationResponse&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
public class PaymentVerificationResponse {

    @JsonProperty("status")
    private String status;

    @JsonProperty("message")
    private String message;

    @JsonProperty("data")
    private Data data;

    @Getter
    @Setter
    @AllArgsConstructor
    @NoArgsConstructor
    @JsonInclude(JsonInclude.Include.NON_NULL)
    @JsonIgnoreProperties(ignoreUnknown = true)
    public static class Data{

        @JsonProperty("status")
        private String status;

        @JsonProperty("reference")
        private String reference;

        @JsonProperty("amount")
        private BigDecimal amount;

        @JsonProperty("gateway_response")
        private String gatewayResponse;

        @JsonProperty("paid_at")
        private String paidAt;

        @JsonProperty("created_at")
        private String createdAt;

        @JsonProperty("channel")
        private String channel;

        @JsonProperty("currency")
        private String currency;

        @JsonProperty("ip_address")
        private String ipAddress;

        @JsonProperty("pricing_plan_type")
        private String pricingPlanType;

        @JsonProperty("created_on")
        private Date createdOn = new Date();

        @JsonProperty("updated_on")
        private Date updatedOn = new Date();
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The PaystackPaymentRepository/Impl package has two repositories.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;AppUserRepositoryImpl&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Repository
public interface AppUserRepositoryImpl extends JpaRepository&amp;lt;AppUser, Long&amp;gt; {
}
PaystackPaymentRepositoryImpl
@Repository
public interface PaystackPaymentRepositoryImpl extends JpaRepository&amp;lt;PaymentPaystack, Long&amp;gt; {
}
Next, we have our service and its implementation.
PaystackService Interface
public interface PaystackService {
    CreatePlanResponse createPlan(CreatePlanDto createPlanDto) throws Exception;
    InitializePaymentResponse initializePayment(InitializePaymentDto initializePaymentDto);
    PaymentVerificationResponse payment Verification(String reference, String plan, Long id) throws Exception;
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;PaystackServiceImpl&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@Service
public class PaystackServiceImpl implements PaystackService {

    private final PaystackPaymentRepositoryImpl paystackPaymentRepository;
    private final AppUserRepositoryImpl appUserRepository;

    @Value("${applyforme.paystack.secret.key}")
    private String paystackSecretKey;

    public PaystackServiceImpl(PaystackPaymentRepositoryImpl paystackPaymentRepository, AppUserRepositoryImpl appUserRepository) {
        this.paystackPaymentRepository = paystackPaymentRepository;
        this.appUserRepository = appUserRepository;
    }

    @Override
    public CreatePlanResponse createPlan(CreatePlanDto createPlanDto) throws Exception {
        CreatePlanResponse createPlanResponse = null;

        try {
            Gson gson = new Gson();
            StringEntity postingString = new StringEntity(gson.toJson(createPlanDto));
            HttpClient client = HttpClientBuilder.create().build();
            HttpPost post = new HttpPost(PAYSTACK_INIT);
            post.setEntity(postingString);
            post.addHeader("Content-type", "application/json");
            post.addHeader("Authorization", "Bearer " + paystackSecretKey);
            StringBuilder result = new StringBuilder();
            HttpResponse response = client.execute(post);

            if (response.getStatusLine(). getStatusCode() == STATUS_CODE_CREATED) {

                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

                String line;
                while ((line = rd.readLine()) != null) {
                    result.append(line);
                }
            } else {
                throw new Exception ("Paystack is unable to process payment at the moment " +
                        "or something wrong with request");
            }

            ObjectMapper mapper = new ObjectMapper();
            createPlanResponse = mapper.readValue(result.toString(), CreatePlanResponse.class);
        } catch(Throwable ex) {
            ex.printStackTrace();
        }
        return createPlanResponse;
    }

    @Override
    public InitializePayment Response initializePayment(InitializePaymentDto initializePaymentDto) {
        InitializePaymentResponse initializePaymentResponse = null;

        try {
            Gson gson = new Gson();
            StringEntity posting String = new StringEntity(gson.toJson(initializePaymentDto));
            HttpClient client = HttpClientBuilder.create().build();
            HttpPost post = new HttpPost(PAYSTACK_INITIALIZE_PAY);
            post.setEntity(postingString);
            post.addHeader("Content-type", "application/json");
            post.addHeader("Authorization", "Bearer " + paystackSecretKey);
            StringBuilder result = new StringBuilder();
            HttpResponse response = client.execute(post);

            if (response.getStatusLine(). getStatusCode() == STATUS_CODE_OK) {

                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

                String line;
                while ((line = rd.readLine()) != null) {
                    result.append(line);
                }
            } else {
                throw new Exception("Paystack is unable to initialize payment at the moment");
            }

            ObjectMapper mapper = new ObjectMapper();
            initializePaymentResponse = mapper. readValue(result.toString(), InitializePaymentResponse.class);
        } catch(Throwable ex) {
            ex.printStackTrace();
        }
        return initializePaymentResponse;
    }

    @Override
    @Transactional
    public PaymentVerification Response paymentVerification(String reference, String plan, Long id) throws an exception. {
        PaymentVerificationResponse paymentVerificationResponse = null;
        PaymentPaystack payment Paystack = null;

        try{
            HttpClient client = HttpClientBuilder.create().build();
            HttpGet request = new HttpGet(PAYSTACK_VERIFY + reference);
            request.addHeader("Content-type", "application/json");
            request.addHeader("Authorization", "Bearer " + paystackSecretKey);
            StringBuilder result = new StringBuilder();
            HttpResponse response = client.execute(request);

            if (response.getStatusLine(). getStatusCode() == STATUS_CODE_OK) {
                BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                String line;

                while ((line = rd.readLine()) != null) {
                    result.append(line);
                }
            } else {
                throw new Exception("Paystack is unable to verify payment at the moment");
            }

            ObjectMapper mapper = new ObjectMapper();
            paymentVerificationResponse = mapper.readValue(result.toString(), PaymentVerificationResponse.class);

if paymentVerificationResponse == null || paymentVerificationResponse getStatus().equals("false")) {
                throw new Exception("An error");
            } else if (paymentVerificationResponse. getData().getStatus().equals("success")) {

                AppUser appUser = appUserRepository.getById(id);
                PricingPlanType pricing PlanType = PricingPlanType.valueOf(plan.toUpperCase());

                paymentPaystack = PaymentPaystack.builder()
                        .user(appUser)
                        .reference(paymentVerificationResponse.getData().getReference())
                        .amount(paymentVerificationResponse.getData().getAmount())
                        .gatewayResponse(paymentVerificationResponse.getData().getGatewayResponse())
                        .paidAt(paymentVerificationResponse.getData().getPaidAt())
                        .createdAt(paymentVerificationResponse.getData().getCreatedAt())
                        .channel(paymentVerificationResponse.getData().getChannel())
                        .currency(paymentVerificationResponse.getData().getCurrency())
                        .ipAddress(paymentVerificationResponse.getData().getIpAddress())
                        .pricingPlanType(pricingPlanType)
                        .createdOn(new Date())
                        .build();
            }
        } catch (Exception ex) {
            throw new Exception("Paystack");
        }
        paystackPaymentRepository.save(paymentPaystack);
        return paymentVerificationResponse;
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Last but not least we have our PaystackController in the controller package.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;@RestController
@RequestMapping(
        value = "/paystack",
        produces = MediaType. APPLICATION_JSON_VALUE
)
public class PaystackController {

    private final PaystackService paystackService;

    public PaystackController(PaystackService paystackService) {
        this.paystackService = paystackService;
    }

    @PostMapping("/createplan")
    public CreatePlanResponse createPlan (@Validated @RequestBody CreatePlanDto createPlanDto) throws Exception {
        return paystackService.createPlan(createPlanDto);
    }

    @PostMapping("/initializepayment")
    public InitializePaymentResponse initializePayment(@Validated @RequestBody InitializePaymentDto initializePaymentDto) throws Throwable {
        return paystackService.initializePayment(initializePaymentDto);
    }

    @GetMapping("/verifypayment/{reference}/{plan}/{id}")
    public PaymentVerification Response paymentVerification(@PathVariable(value = "reference") String reference,
@PathVariable (value = "plan") String plan,
                                                           @PathVariable(value = "id") Long id) throws Exception {
        if (reference.isEmpty() || plan.isEmpty()) {
            throw new Exception("reference, plan and id must be provided in path");
        }
        return paystackService.paymentVerification(reference, plan, id);
    }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the PaystackServiceImpl uses the Paystack secret key from the application properties file. Prior to using a live key that will be provided once you have submitted relevant business-related documentation, Paystack has provided you with this test key in order to test your implementation.&lt;/p&gt;

&lt;p&gt;Visit the &lt;a href="https://paystack.com/docs/" rel="noopener noreferrer"&gt;Paystack &lt;/a&gt;documentation page to further utilize the payment gateway.&lt;/p&gt;

&lt;p&gt;There you go, You have successfully integrated the Paystack payment gateway. You can find all the code in this &lt;a href="https://github.com/omoluabidotcom/PaystackIntegrationAPI" rel="noopener noreferrer"&gt;GitHub &lt;/a&gt; repository.&lt;/p&gt;

&lt;p&gt;I'll stop writing now because this article is already quite long.&lt;/p&gt;

&lt;p&gt;I'll be releasing a follow-up, that will show you how to test your APIs using Postman.&lt;/p&gt;

&lt;p&gt;Please leave any questions you may have in the comment section.&lt;br&gt;
Connect with me on &lt;a href="https://www.linkedin.com/in/yahaya-yusuf-76a3231b0/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt; and &lt;a href="https://twitter.com/Omoluab_i" rel="noopener noreferrer"&gt;Twitter&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Credit&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://paystack.com/docs/" rel="noopener noreferrer"&gt;Paystack Documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>spring</category>
      <category>api</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Command Line Interface for Beginners</title>
      <dc:creator>omoluabidotcom</dc:creator>
      <pubDate>Sun, 25 Dec 2022 10:43:12 +0000</pubDate>
      <link>https://dev.to/omoluabidotcom/command-line-interface-for-beginners-4hjj</link>
      <guid>https://dev.to/omoluabidotcom/command-line-interface-for-beginners-4hjj</guid>
      <description>&lt;p&gt;Command Line Interface or CLI as it usually refers to is an interface/tool that allows interaction with the hardware component of the computer. CLI however, does this through commands written in the console or terminal.&lt;/p&gt;

&lt;p&gt;GUI or Graphical User Interface is also an option provided by the computer Operating System for a user to interact with the hardware, GUI is beginner friendly but the CLI, on the other hand, has a steep learning curve.&lt;/p&gt;

&lt;p&gt;In this article, you will get introduced to CLI and start to use it to perform basic computer operations you have been performing with GUI. Without much ado let's get started.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Prerequisite&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic computer operation knowledge&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;What you will learn&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Create a file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;View list of directory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;View file content&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Delete a file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Create and Deleting directory&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Copy file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Move file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Rename file&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Navigate through folders/directory&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Create a file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Let's create your first file using the console. Open your Command prompt if you are on windows and Terminal for MacBook users.&lt;/p&gt;

&lt;p&gt;Write in your console the command below and press enter key to execute. You would notice a blinking underscore; this is because the "copy con {filename}" allows you to add content to your file immediately after you create one. Press Ctrl C to exit editing.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;copy con firstfile.txt

This is my firstfile created on Console
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;View list of Directory&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Write the command below in your console and press enter to view all directories and files in the current directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;dir&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;View file content&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Write the command below to display the content of a file to the console.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;type &lt;/span&gt;firstfile.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Delete a file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To delete a file you write the command below followed by the name of the file you want to delete, since we have created "firstfile.txt" the command below should work fine.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rd firstfile.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Creating and deleting a directory&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The following commands are for creating and deleting a directory.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;// to create directory firstdirectory
md firstdirectory

// to delete directory firstdirectory
rd firstdirectory
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Copy file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Copying a file in the console is simple you use the copy keyword and specify where to copy from and where to copy to. If the destination "secondfile.txt" isn't existing the command will create it and copy to destination.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;copy firstfile.txt secondfile.txt

//To view the copied content us the &lt;span class="nb"&gt;command &lt;/span&gt;below
&lt;span class="nb"&gt;type &lt;/span&gt;secondfile.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Move file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Command moving a file is similar to copying a file. Use the move keyword, the example below will move the "firstfile.txt" to the "firstdirectory" created earlier.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;move firstfile.txt firstdirectory

//To cross check &lt;span class="k"&gt;if &lt;/span&gt;the move was successfully write the &lt;span class="nb"&gt;dir command&lt;/span&gt;, the //firstfile.txt won&lt;span class="s1"&gt;'t be visible again from list because it has been moved
dir
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Rename file&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Command to rename a file uses the rename keyword. the example renames the "secondfile.txt" to "thirdfile.txt".&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;rename secondfile.txt thirdfile.txt
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Navigate through folders/directory&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You use the cd command which means changing the directory to navigate around the folders. Earlier we moved "firstfile.txt" to the directory "firstdirectory" now let's change directory to the firstdirectory and see if the file is actually there&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;firstdirectory

//Then enter &lt;span class="nb"&gt;dir &lt;/span&gt;to view directory content
&lt;span class="nb"&gt;dir&lt;/span&gt;

//To go back to previous directory
&lt;span class="nb"&gt;cd&lt;/span&gt; ..
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You have come to the end of the introduction to Command Line Interface for beginners. You have learned to perform basic computer operations with the CLI, like creating a file/directory, deleting a file/directory, moving, renaming, copying a file, and navigating the computer system.&lt;/p&gt;

&lt;p&gt;if you do learn one or refresh your memory of one or two things do drop a like and share.&lt;/p&gt;

&lt;p&gt;Connect with me on &lt;a href="https://www.linkedin.com/in/yahaya-yusuf-76a3231b0/" rel="noopener noreferrer"&gt;Linkedin.&lt;/a&gt; Thank you for your time. have a nice day.&lt;/p&gt;

</description>
      <category>welcome</category>
      <category>beginners</category>
      <category>learning</category>
    </item>
    <item>
      <title>Using Predicate from the Functional Interface Package in Java</title>
      <dc:creator>omoluabidotcom</dc:creator>
      <pubDate>Wed, 14 Sep 2022 18:50:44 +0000</pubDate>
      <link>https://dev.to/omoluabidotcom/using-predicate-from-the-functional-interface-package-in-java-1np9</link>
      <guid>https://dev.to/omoluabidotcom/using-predicate-from-the-functional-interface-package-in-java-1np9</guid>
      <description>&lt;p&gt;The advent of Java 8 brought about java.util.function package in Java. It's a package that contains defined functional interfaces usually used with Lambda Expression.&lt;/p&gt;

&lt;p&gt;I'm assuming that you have basic knowledge of Lambda Expression if not go ahead and read this article &lt;a href="https://dev.to/omoluabidotcom/lambda-expression-simplified-5flf"&gt;here&lt;/a&gt; and then come back to this one.&lt;/p&gt;

&lt;p&gt;This is the first of a series of articles I will be publishing on how to make use of Functional Interfaces from the package.&lt;/p&gt;

&lt;p&gt;Let's get started!!!&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Predicate&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Predicate according to JDK 1.8 documentation "represents a predicate(Boolean-valued function) of one argument"; what this refers to basically is that you make use of Predicate whenever you need to check certain conditions that need to return a Boolean.&lt;/p&gt;

&lt;p&gt;Predicate Interface has five methods, a single abstract method as expected; three default methods, and a static method.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Methods from the Predicate Interface&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;test() method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;test() method is also referred to as a functional method of the functional interface Predicate. test() method takes in a single argument evaluates this predicate on the given argument and returns a Boolean.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Excerpt from documentation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// predicate interface type assigned to a field
Predicate&amp;lt;Integer&amp;gt; p = n -&amp;gt; (n&amp;gt;20);
// calling the functional method test on the predicate field p
System.out.println(p.test(40)); // output = true

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Above you see we created a field p of type Predicate. We assigned a lambda expression that checks if the argument passed is greater than 20 to the field p. When the functional method test() is called on any of the methods the parameter passed in is inferred and used in place of n from the lambda expression, in the case (i.e. the example) the answer is true since 40 is greater than 20.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;and() method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;One of the three default methods from the Predicate Interface is and() method. It takes in one argument of type Predicate while the other is chained with the and() method using the dot operator like so.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// syntax to use and() method
P1.and(p2);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and() method "returns a composed predicate that represents a shorts-circuiting logical AND of his predicate and another". What this simply means is that and() method evaluates two predicates by combining their result together using the AND logical expression to get an output.&lt;br&gt;
While evaluating if any of the predicates is false, then the other is not evaluated as both have to be true; therefore in this particular case false is the output.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Excerpt from documentation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// array of number
int[] numArray = {2, 5, 10, 20, 30, 40, 45, 47, 50, 56, 60, 63, 70};
// to check if a number is even
Predicate&amp;lt;Integer&amp;gt; p1= n -&amp;gt; (n%2==0);
// to check id number is greater than 55
Predicate&amp;lt;Integer&amp;gt; p2= n -&amp;gt; (n&amp;gt;55);
// looping through number array
for (int num : numArray) {
// using and() method to check for two conditions and calling the functional method test()
if(p1.and(p2).test(num)) {
// print numbers that are even and greater than 55
System.out.println(num);
}
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;or() method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;or() method in many ways is similar to and() method. It takes in one argument of type Predicate while the other is combine with it using the dot operator like so.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// syntax to use or() method
p1.or(p2);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;According to Java 8 documentation "It returns a composed predicate that represents a short-circuiting logical OR of this predicate and another.&lt;br&gt;
What this simply means is that or() method evaluates predicates by combining their result to give an output.&lt;br&gt;
While evaluating the Predicate using the or method if the first gives a true the other one is not evaluated since or() only needs one of the logic to be true.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Excerpt from documentation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// array of numbers
int[] numArray = {2, 5, 10, 20, 30, 40, 45, 47, 50, 56, 60, 63, 70};
// to check if a number is even
Predicate&amp;lt;Integer&amp;gt; p1= n -&amp;gt; (n%2==0);
// to check id number is greater than 55
Predicate&amp;lt;Integer&amp;gt; p2= n -&amp;gt; (n&amp;gt;55);
// looping through array
for (int num : numArray) {
// using or() method to check for two conditions and calling the functional method test()
if(p1.or(p2).test(num)) {
// print numbers that are either even or greater than 55
System.out.println(num);
}
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;negate() method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;negate() method returns the opposite of the Predicate result, negate() is straightforward. According to documentation "Returns a predicate that represents the logical negation of this predicate"&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// array of numbers
int[] numArray = {2, 5, 10, 20, 30, 40, 45, 47, 50, 56, 60, 63, 70};
// to check if a number is even
Predicate&amp;lt;Integer&amp;gt; p1= n -&amp;gt; (n%2==0);
// to check id number is greater than 55
Predicate&amp;lt;Integer&amp;gt; p2= n -&amp;gt; (n&amp;gt;55);
// looping through array
for (int num : numArray) {
// using negate() method get negation of two conditions and calling the functional method test()
if(p1.negate(p2).test(num)) {
// print numbers that are neither even nor greater than 55
System.out.println(num);
}
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;isEqual() method&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;This method function like the Objects.equals(Object, Object) method to compare two objects and check if they are equal. Just like the rest of the method apart from the functional method test. isEqual() method returns a predicate that represents if the two objects are equal or not.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Excerpt from documentation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Sample code&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// predicate type field p1 using the isEqual() method
Predicate&amp;lt;String&amp;gt; p1 = Predicate.isEqual("Prince");
// invocating the isEqual() using the functional method test()
System.out.println(p1.test("Prince"));

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Predicate Interface has five methods including the functional method (i.e. test() method) which is the single abstract method, whose function is to invoke any method used from the Predicate interface.&lt;br&gt;
Other methods like or() perform OR logic, and() method perform AND logic, negate() method perform NOT logic and isEqual() method compare two Predicate object and give a Boolean output which reflects the object equality.&lt;/p&gt;

&lt;p&gt;Thank you for your time and catch me on the next one, connect with me on &lt;a href="https://www.linkedin.com/in/yahaya-yusuf-76a3231b0/"&gt;LinkedIn&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Credit&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/Predicate.html"&gt;Java 8 Documentation&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://www.geeksforgeeks.org/java-8-predicate-with-examples/"&gt;GeeksforGeeks&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>java</category>
      <category>predicateinterface</category>
      <category>funtionalprogramming</category>
      <category>interface</category>
    </item>
    <item>
      <title>Lambda Expression Simplified</title>
      <dc:creator>omoluabidotcom</dc:creator>
      <pubDate>Wed, 24 Aug 2022 02:09:46 +0000</pubDate>
      <link>https://dev.to/omoluabidotcom/lambda-expression-simplified-5flf</link>
      <guid>https://dev.to/omoluabidotcom/lambda-expression-simplified-5flf</guid>
      <description>&lt;p&gt;In the release of Java 8, the Lambda Expression was introduced. It's a technique for writing clear and succinct code. One of those concepts that people, especially novices, find challenging to understand is Lambda Expression. You will learn about Lambda expressions in this article, including their syntax, uses, and justifications.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Overview of the Lambda Expression&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;To better grasp the vocabulary and concepts in this article, I do advise you to have some familiarity with Java before reading it, especially basic knowledge of the Interface.&lt;/p&gt;

&lt;p&gt;Java is an object-oriented programming language. However, the introduction of Lambda Expression in Java enabled us to write Java code using the functional programming paradigm.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;The following two factors play a vital role in the existence of Lambda Expressions:&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Introduce Functional Programming in Java.&lt;/li&gt;
&lt;li&gt;Improve clear and concise coding in Java&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;The Java interface&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Java's concept of interfaces is that they represent a contract between a class and the particular interface that has been implemented, requiring that the class supply its own implementation of the interface's methods. Example below&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;// Interface Animal
public interface Animal {

    public String talking();
}

// Class Sheep with implementation
public class Sheep implements Animal{

    public String talking() {

        return "Meaeeeeeeeee";
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, in order to call this method, we must first create an object of the class Sheep and then use that object to call the method as shown in the sample code below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// Sheep object
Sheep sheepObj = new Sheep(); 

// calling talking method
sheepObj.talking();

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A different approach is to write a method sheepTalking and make the argument to be passed of type Animal.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
public String sheepTalking(Animal animalTalk) {

    return animalTalk.talking();
}


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Therefore, this method accepts any object from a class that implements the interface, as seen in the example below.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// Sheep object
Sheep sheepObj = new Sheep(); 

// Sheep object is passed as parameter
sheepObj.sheepTalking(sheepObj);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In essence, the way the method that accepts Sheep class objects is written, it appears as though we are providing the method's implementation directly into another method as an argument like so.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// Depict how it look
  sheepObj.sheepTalking(
            public String talking() {

                return "Meaeeeeeeeee";
        }
        );
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Lambda Expressions Syntax&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Lambda expression is the main topic of our attention. The implementation of a method can be passed in as an argument using lambda expressions. The process is similar to what we did above but with a different syntax we provide the brace for argument and add a dash and greater than sign before the curly brackets as shown; the access modifier, return type, and the method name is not given.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
// Lambda Expression
sheepObj.sheepTalking( () -&amp;gt; {
             return "Meaeeeeeeeee";
            }
        );

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see, the syntax has been changed to remove the access modifier, omit the return type, and leave out the method name; that’s how to write lambda in Java.&lt;/p&gt;

&lt;p&gt;*&lt;em&gt;Functional Interface *&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Functional interfaces are interfaces with a single abstract method. Single Abstract Method Interface, or SAM Interface, is another name for them. See sample code for illustration.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;
@FunctionalInterface
public interface Animal {

    public String talking();
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The term "abstract method" refers to a method without defined implementation in the interface. As a result, a functional interface is one that has just one abstract method. &lt;strong&gt;Lambda Expression is only compatible with Functional interface any other Interface can't be use with Lambda Expression.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Although not required, the annotation @FuntionalInterface is advised, particularly in large projects to alert other developers working on the codebase to prevent updates that might break the code.&lt;br&gt;
You can design your own functional interfaces, but Java already has several built-in, so it's best to verify before writing any custom ones.&lt;/p&gt;

&lt;p&gt;Using the built-in SAM interface for Java is outside the scope of this article, but the Java.until.function package includes &lt;a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html"&gt;built-in Functional Interfaces&lt;/a&gt; for Java. I'll talk about this package and discuss the more popular interfaces in a later article.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Summary&lt;/strong&gt;&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;To pass a behavior or method as a parameter, we use a lambda expression.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;The syntax for lambda expressions&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;No access modifier&lt;/li&gt;
&lt;li&gt;Omit return type&lt;/li&gt;
&lt;li&gt;Method name should not be added&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;Lambda Expression makes our code short, concise, and readable.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Please use the comment area if you have any questions. Connect with me on &lt;a href="https://twitter.com/Omoluab_i"&gt;Twitter&lt;/a&gt; and &lt;a href="https://www.linkedin.com/in/yahaya-yusuf-76a3231b0/"&gt;LinkedIn&lt;/a&gt;.&lt;br&gt;
Have a good day and thanks for your time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Credit&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://docs.oracle.com/javase/tutorial/java/javaOO/lambdaexpressions.html"&gt;Java 8 documentation&lt;/a&gt;&lt;/p&gt;

</description>
      <category>java</category>
      <category>backend</category>
      <category>lambdaexpression</category>
      <category>cleancode</category>
    </item>
  </channel>
</rss>
