<?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: Eugen (Baeldung)</title>
    <description>The latest articles on DEV Community by Eugen (Baeldung) (@baeldung).</description>
    <link>https://dev.to/baeldung</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%2F299969%2Fed37712a-b5da-4cac-8218-55d66929c347.jpeg</url>
      <title>DEV Community: Eugen (Baeldung)</title>
      <link>https://dev.to/baeldung</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/baeldung"/>
    <language>en</language>
    <item>
      <title>Three Simple Examples of the Find Command</title>
      <dc:creator>Eugen (Baeldung)</dc:creator>
      <pubDate>Fri, 17 Jul 2020 18:27:27 +0000</pubDate>
      <link>https://dev.to/baeldung/three-simple-examples-of-the-find-command-3g66</link>
      <guid>https://dev.to/baeldung/three-simple-examples-of-the-find-command-3g66</guid>
      <description>&lt;h1&gt;
  
  
  1. Overview
&lt;/h1&gt;

&lt;p&gt;Looking for files is a common operation when we work in the Linux command-line. The find command is a handy utility to find files.&lt;/p&gt;

&lt;p&gt;In this short tutorial, we'll have a look at three simple but practical examples of the find &lt;a href="https://www.baeldung.com/linux/find-command"&gt;command&lt;/a&gt;.&lt;/p&gt;

&lt;h1&gt;
  
  
  2. Find Files by a Filename Pattern
&lt;/h1&gt;

&lt;p&gt;Searching for files matching a particular name or name pattern could be the most common use-case of the find command. Using the find command, we can pass the -name option and a filename pattern to locate the files.&lt;/p&gt;

&lt;p&gt;For example, we can use the -name option to find all json files under our Python installation directory /usr/lib/python3.8:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ find /usr/lib/python3.8 -name '*.json' 
/usr/lib/python3.8/site-packages/glances/outputs/static/package.json
/usr/lib/python3.8/site-packages/glances/outputs/static/package-lock.json
/usr/lib/python3.8/site-packages/compose/config/config_schema_v3.5.json
/usr/lib/python3.8/site-packages/compose/config/config_schema_v3.8.json
/usr/lib/python3.8/site-packages/compose/config/config_schema_v2.0.json
...
/usr/lib/python3.8/site-packages/jsonschema/schemas/draft3.json
/usr/lib/python3.8/site-packages/jsonschema/schemas/draft6.json
/usr/lib/python3.8/site-packages/zmq/utils/config.json
/usr/lib/python3.8/site-packages/zmq/utils/compiler.json
/usr/lib/python3.8/site-packages/guzzle_sphinx_theme/guzzle_sphinx_theme/static/fonts/source-serif-pro/bower.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The command is pretty straightforward. But it's worthwhile to mention that when we use the -name option with a pattern, the find command will match against basenames only — that is, the leading directories will be ignored.&lt;/p&gt;

&lt;p&gt;For example, if we want to find the two json files under the */utils/ directory, this command will fail:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ find /usr/lib/python3.8 -name '*/utils/*.json'
find: warning: ‘-name’ matches against basenames only, but the given pattern contains a directory separator (‘/’),
thus the expression will evaluate to false all the time.  Did you mean ‘-wholename’?
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As the warning message suggested, we can use the option -wholename to do the search with leading directories:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ find /usr/lib/python3.8 -wholename '*/utils/*.json'                                    
/usr/lib/python3.8/site-packages/zmq/utils/config.json
/usr/lib/python3.8/site-packages/zmq/utils/compiler.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;It works! The expected result is printed.&lt;/p&gt;

&lt;p&gt;However, it's worth noting that the -wholename option is GNU-specific. This option is not supported by HP-UX find and not a part of the POSIX 2008 standard.&lt;/p&gt;

&lt;p&gt;A more portable way to match leading directories and basenames is to use the -path option:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ find /usr/lib/python3.8 -path '*/utils/*.json'
/usr/lib/python3.8/site-packages/zmq/utils/config.json
/usr/lib/python3.8/site-packages/zmq/utils/compiler.json
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;h1&gt;
  
  
  3. Find Files by Size
&lt;/h1&gt;

&lt;p&gt;Sometimes, we want to find files that are larger or smaller than a certain size. We can use the -size option of the find command to do exactly that.&lt;/p&gt;

&lt;p&gt;Let's find all files larger than 5MB under the Python installation directory:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ find /usr/lib/python3.8 -size '+5M'
/usr/lib/python3.8/config-3.8-x86_64-linux-gnu/libpython3.8.a
/usr/lib/python3.8/site-packages/PyQt5/QtWidgets.abi3.so
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The -size option requires a "+/-n[UNIT]" parameter to do the search.&lt;/p&gt;

&lt;p&gt;The "+" stands for greater than, while the "-" means less than. The --size  option supports six units:&lt;/p&gt;

&lt;p&gt;b: 512-byte blocks&lt;br&gt;
c: bytes&lt;br&gt;
w: two-byte words&lt;br&gt;
k: kilobytes&lt;br&gt;
M: megabytes&lt;br&gt;
G: gigabytes&lt;/p&gt;
&lt;h1&gt;
  
  
  4. Find Files with the Same Filename
&lt;/h1&gt;

&lt;p&gt;In the last example, let's find out the files with the same filename.&lt;/p&gt;

&lt;p&gt;We'll find all files matching the "README.*" pattern under the Python installation directory, grouping together those with the same filename:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ find /usr/lib/python3.8 -name 'README.*' | awk -F'/' 'a[$NF]++{print $NF" -&amp;gt; "$0}' | sort
README.md -&amp;gt; /usr/lib/python3.8/site-packages/dulwich/contrib/README.md
README.md -&amp;gt; /usr/lib/python3.8/site-packages/guzzle_sphinx_theme/guzzle_sphinx_theme/static/fonts/source-serif-pro/README.md
README.txt -&amp;gt; /usr/lib/python3.8/idlelib/Icons/README.txt
...
README.txt -&amp;gt; /usr/lib/python3.8/site-packages/zc/lockfile/README.txt
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;In this example, we passed the output of the find command to an awk one-liner. The awk one-liner is responsible for finding the files with duplicate names. Finally, the sort command groups the results.&lt;/p&gt;

&lt;p&gt;Using the find command, not only can we find files that have the same names, but we can also find &lt;a href="https://www.baeldung.com/linux/finding-duplicate-files"&gt;duplicate files&lt;/a&gt; as well. &lt;/p&gt;

&lt;h1&gt;
  
  
  5. Conclusion
&lt;/h1&gt;

&lt;p&gt;The find command is a common Linux command-line utility. In this short article, we had a look at three simple examples of this command.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>shell</category>
    </item>
    <item>
      <title>The Adapter Pattern in Java</title>
      <dc:creator>Eugen (Baeldung)</dc:creator>
      <pubDate>Tue, 24 Dec 2019 08:35:20 +0000</pubDate>
      <link>https://dev.to/baeldung/the-adapter-pattern-in-java-4e6c</link>
      <guid>https://dev.to/baeldung/the-adapter-pattern-in-java-4e6c</guid>
      <description>&lt;h1&gt;
  
  
  &lt;strong&gt;1. Overview&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;In this quick tutorial, we'll have a look at the Adapter pattern and its Java implementation.&lt;/p&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;2. The Adapter Pattern&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;An Adapter pattern acts as a connector between two incompatible interfaces that otherwise cannot be connected directly.&lt;/strong&gt; An Adapter wraps an existing class with a new interface so that it becomes compatible with the client’s interface.&lt;/p&gt;

&lt;p&gt;The main motive behind using this pattern is to convert an existing interface into another interface that the client expects. It's usually implemented once the application is designed.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;2.1. Adapter Pattern Example&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;Consider a scenario in which there is an app that's developed in the US which returns the top speed of luxury cars in miles per hour (MPH). Now we need to use the same app for our client in the UK that wants the same results but in kilometers per hour (km/h).&lt;/p&gt;

&lt;p&gt;To deal with this problem, we'll create an adapter which will convert the values and give us the desired results:&lt;/p&gt;

&lt;p&gt;First, we'll create the original interface Movable which is supposed to return the speed of some luxury cars in miles per hour:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;Movable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// returns speed in MPH &lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getSpeed&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;We'll now create one concrete implementation of this interface:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;BugattiVeyron&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;Movable&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getSpeed&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;268&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we'll create an adapter interface MovableAdapter that will be based on the same &lt;code&gt;Movable&lt;/code&gt; class. It may be slightly modified to yield different results in different scenarios:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;MovableAdapter&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="c1"&gt;// returns speed in KM/H &lt;/span&gt;
    &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getSpeed&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;The implementation of this interface will consist of private method &lt;code&gt;convertMPHtoKMPH()&lt;/code&gt; that will be used for the conversion:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kd"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;MovableAdapterImpl&lt;/span&gt; &lt;span class="kd"&gt;implements&lt;/span&gt; &lt;span class="nc"&gt;MovableAdapter&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="nc"&gt;Movable&lt;/span&gt; &lt;span class="n"&gt;luxuryCars&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

    &lt;span class="c1"&gt;// standard constructors&lt;/span&gt;

    &lt;span class="nd"&gt;@Override&lt;/span&gt;
    &lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;getSpeed&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nf"&gt;convertMPHtoKMPH&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;luxuryCars&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getSpeed&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;

    &lt;span class="kd"&gt;private&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;convertMPHtoKMPH&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;mph&lt;/span&gt;&lt;span class="o"&gt;)&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;mph&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;1.60934&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;
    &lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;Now we'll only use the methods defined in our Adapter, and we'll get the converted speeds. In this case, the following assertion will be true:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight"&gt;&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@Test&lt;/span&gt;
&lt;span class="kd"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;whenConvertingMPHToKMPH_thenSuccessfullyConverted&lt;/span&gt;&lt;span class="o"&gt;()&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="nc"&gt;Movable&lt;/span&gt; &lt;span class="n"&gt;bugattiVeyron&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;BugattiVeyron&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt;
    &lt;span class="nc"&gt;MovableAdapter&lt;/span&gt; &lt;span class="n"&gt;bugattiVeyronAdapter&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;MovableAdapterImpl&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bugattiVeyron&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

    &lt;span class="n"&gt;assertEquals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;bugattiVeyronAdapter&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;getSpeed&lt;/span&gt;&lt;span class="o"&gt;(),&lt;/span&gt; &lt;span class="mf"&gt;431.30312&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.00001&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;



&lt;p&gt;As we can notice here, our adapter converts 268 mph to 431 km/h for this particular case.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;2.2. When to Use Adapter Pattern&lt;/strong&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;When an outside component provides captivating functionality that we'd like to reuse, but it's incompatible with our current application. A suitable Adapter can be developed to make them compatible with each other&lt;/li&gt;
&lt;li&gt;When our application is not compatible with the interface that our client is expecting&lt;/li&gt;
&lt;li&gt;When we want to reuse legacy code in our application without making any modification in the original code&lt;/li&gt;
&lt;/ul&gt;

&lt;h1&gt;
  
  
  &lt;strong&gt;3. Conclusion&lt;/strong&gt;
&lt;/h1&gt;

&lt;p&gt;In this article, we had a look at the Adapter design pattern in Java.&lt;/p&gt;

&lt;p&gt;The full source code for this example is available &lt;a href="https://github.com/eugenp/tutorials/tree/master/patterns/design-patterns-structural"&gt;over on GitHub&lt;/a&gt;.&lt;/p&gt;

</description>
      <category>java</category>
      <category>pattern</category>
    </item>
  </channel>
</rss>
