- orElseThrow: if personService.getAll() returns an Optional, then:
return personService.getAll().map(converter::toResource).orElseThrow(
() -> new NoResourceFound(...)
); - orElse: if personService.getByLastName() returns an optional:
return personService.getByLastName(lName).orElse(null);
Monday, May 20, 2019
Java Optional - Good Practice
Tuesday, April 30, 2019
Java Streams
Null elements in the stream
I thought, I saw a statement that Java streams filters out the null elements in the stream but the information must have been wrong. So, if nulls are expected, or just in case, do:
... .filter(Objects::nonNull). ....
Monday, April 29, 2019
Java 9 - What's New In It
Modules
- defined by file module-info.java in the module root directory
- defines the module name, which package the module exports and
what other modules it requires - if package org.sjb exports to a module, its subpackage isn't
exported automatically; it can be exported by any module - each package is exported by a single module
- jlink - packages a modules with all its required modules and the needed parts of the JRE into a standalone app which won't need Java to be installed where it runs
- unnamed modules - all not defined stuff gets packed into a single unnamed module. It exports all packages but only unnamed modules can call them.
- automatic modules ...
Services
Using modules, the client code can receive all available implementations
of an interface, and choose as one it needs (server and client are in the same JVM;
it's not about http clients)
Monday, March 18, 2019
jUnit Fun
jUnit and Mockito Fun
More Interesting Elements of Unit Testing and Mocking
- Verify that a method was called on a mock
- Verify the parameters the method was called with
- Return a custom response from a method called on a mock, response based on the parameters of the call
Mockito.when(myMock.save(any(Person.class))).thenAnswer(i -> { Person person = (MyClass) i.getArguments()[0]; return "Hello, " + person.getFirstName(); });
Friday, September 14, 2018
AWK - Just Examples with Explanations
Example 1
/ERROR/ { if ($0 !~ /notThisOne/) print NR $0 }
This script prints all lines with the word ERROR but without a word notThisOne. The original line number is printed in front of each line printed.
Friday, October 13, 2017
Git - Basic Commands
Understanding
The Git repository is a tree of commits each of which keeps a pointer to its parent (for merge commits: two parents) and all of the files modified in this commit. HEAD, tags and branches are all labels to a node of that tree.Branches
- git branch NAME --- create a branch of the current branch
- git branch --all --- list all branches local and remote
- git checkout -b release-4.16.0 origin/develop --- create branch release-4.16.0 from the current branch and set tracking to origin/develop
- git branch -u origin/develop --- set tracking on an existing branch
- git branch -d NAME - delete a local branch but only if it's safe (i.e. we won't loose access to any commit)
- git branch -D NAME - force delete a local branch (requires extra permissions; commits to which we may loose access will be available through its ID for next N days (default is 30 days) and it will be garbage-collected after that)
- git push origin --delete NAME - force delete a remote branch
Commit
- git commit --- committing the changes to local git repo
- git commit --amend --- allows to edit the last commit, to edit its message and correct the files you committed
- git reset --soft HEAD~1 --- undoes the most recent commit without reverting the changes in the files
- git fetch origin && git reset --hard origin/master --- reset your branch to what it was before any changes and commits done locally. To save the local commits just in case, do git branch saved-my-commits-here.
Pushing and Pull
- git add --- staging files for commit
- git pull --- if the current branch is behind the branch it tracks, this command will update it
- git push origin local-branch:remote-branch --- push local branch to remote repository origin
Rebase
- git rebase origin/develop --- rebases, which means if the current branch is behind on some commits from origin/develop branch, it will incorporate all of them and place as if they were made earlier than those commits that the current branch is ahead of the origin/develop.
- git rebase -i HEAD~2 --- interactive rebase, that is a tool to modify commit history. In most common scenario it is used to squash two or more recent commits into one (though commit --amend can do it, too) but it can also remove commits from the history as well as change their order. The tool can do lasting damage if you make mistake - use with caution!
Tags
- git tag v4.23.0 --- adds a tag v4.23.0 to the current node (there is also another, lighter type of tag - I haven't been using it)
- git push origin v4.23.0 --- pushing a tag to the remote repo
Other
- git fetch --- updates the local git repo from the remote repos
- git status --- shows the commits that the current branch is ahead and behind with respect to the branch it tracks as well as unversioned and modified files that aren't added to the change set.
- git log --- shows a paged list of commits with the most recent first
- cherrypicking --- allows to copy a commit from into another branch (important: it's a copy)
- git merge my-other-branch --- merges my-other-branch into the current branch; use --no-ff if you always want a merge and not just a git pull if the last commit on current branch happens to be an ancestor of the commits on my-other-branch.
- git mv --- rename or move a file or folder without losing its history (to see its full history afterwards use git log --follow).
Tuesday, September 26, 2017
Docker - All the Magic
Understanding It
- docker is a history recorded of what changed from the original image that the container started off
- restart (or stop and start) reverts the container to what the image was, so the container is transient by default
- it may be also possible to revert by tagging the image (yes!)
Maintenance
When it gets sloooow, and the com.docker.hyperkit takes 20% or 30% of CPU or more, try:
- consider reducing the # of cpu's enabled
- prune images not referenced by any container (created
more than 24h ago):docker image prune -a --filter "until=24h" - prune containers all stopped containers (older
than 24h):
docker container prune --filter "until=24h" - prune networks not used by any container (older
than 24h):
docker network prune --filter "until=24h" - and possibly prune volumes not used by currently
by any container (make sure you don't need the
data on them):
docker volume prune
Subscribe to:
Posts (Atom)
