Modelmapper:如何在源对象为空时应用自定义映射?(Modelmapper: How to apply custom mapping when source object is null?)
让我们假设我有班
MySource
:public class MySource { public String fieldA; public String fieldB; public MySource(String A, String B) { this.fieldA = A; this.fieldB = B; } }
我想将它翻译成
MyTarget
对象:public class MyTarget { public String fieldA; public String fieldB; }
使用默认的ModelMapper设置我可以通过以下方式实现它:
ModelMapper modelMapper = new ModelMapper(); MySource src = new MySource("A field", "B field"); MyTarget trg = modelMapper.map(src, MyTarget.class); //success! fields are copied
但是,它可能会发生,
MySource
对象将为null
。 在这种情况下,MyTarget也将为null
:ModelMapper modelMapper = new ModelMapper(); MySource src = null; MyTarget trg = modelMapper.map(src, MyTarget.class); //trg = null
我想用这种方式指定自定义映射,即(伪代码):
MySource src != null ? [perform default mapping] : [return new MyTarget()]
有没有人有想法如何写一个合适的转换器来实现?
Lets assume I have class
MySource
:public class MySource { public String fieldA; public String fieldB; public MySource(String A, String B) { this.fieldA = A; this.fieldB = B; } }
and I would like to translate it to object
MyTarget
:public class MyTarget { public String fieldA; public String fieldB; }
using default ModelMapper settings I can achieve it in the following way:
ModelMapper modelMapper = new ModelMapper(); MySource src = new MySource("A field", "B field"); MyTarget trg = modelMapper.map(src, MyTarget.class); //success! fields are copied
It can happen however, that
MySource
object will benull
. In this case, MyTarget will be alsonull
:ModelMapper modelMapper = new ModelMapper(); MySource src = null; MyTarget trg = modelMapper.map(src, MyTarget.class); //trg = null
I would like to specify custom mapping in such a way, that (pseudo code):
MySource src != null ? [perform default mapping] : [return new MyTarget()]
Does anybody have idea how to write an appropriate converter to achieve that?
原文:https://stackoverflow.com/questions/37971193
最满意答案
如果你刚刚参加,你可以删除.git。 典型:
rm -rf .git
然后,重新创建为正确的用户。
You can just delete .git. Typically:
rm -rf .git
Then, recreate as the right user.
相关问答
更多-
由于git-flow只是包装正常的git功能,所以简单地删除新的分支(假设你没有要保存的变化)? $ git checkout master Switched to branch 'master' $ git branch -d feature/your-feature-name-here Since git-flow just wraps regular git functionality, how about simply deleting the new branch (assuming you h ...
-
“git init”和“git init”之间有什么区别?(What is the difference between “git init” and “git init --bare”?)[2022-08-05]
非are it回复 一个创建一个具有工作目录的存储库,所以你可以实际工作( git clone )。 创建后,您将看到该目录包含一个.git文件夹,其中历史记录和所有git管道都将进行。 您在.git文件夹的级别工作。 Bare Git Repo 另一个创建一个没有工作目录的存储库( git clone --bare )。 你没有找到可以工作的目录。 目录中的所有内容现在都是上述的内容。 为什么你会使用一个与另一个 对于没有工作目录的git repos的需要,您可以将分支推送到它,而不管什么人正在工作。 您 ... -
如果你刚刚参加,你可以删除.git。 典型: rm -rf .git 然后,重新创建为正确的用户。 You can just delete .git. Typically: rm -rf .git Then, recreate as the right user.
-
(代表OP发布) 。 使用fish_config编辑器更新了我的鱼提示,问题已修复! (Posted on behalf of the OP). Updated my fish prompt using the fish_config editor, and the problem has been fixed!
-
Git init创建一个没有任何提交的回购。 由于分支是指向提交的指针,因此在提交内容之前不能有分支。 Git init creates a repo without any commits. Since a branch is a pointer to a commit, you can't have branches until you commit something.
-
这是Bash和其他Bourne兼容shell的一个特性: VAR=val cmd 运行cmd ,将VAR设置为val ,否则运行shell本身的环境。 尝试运行以下命令: printenv export FOO=bar printenv | grep '^FOO=' FOO=baz printenv | grep '^FOO=' This is a feature of Bash and other Bourne-compatible shells: VAR=val cmd runs cmd with ...
-
有了这样的早期版本的git,您仍然可以使用以下命令创建一个裸存储库: git --bare init ...而不是更常见的git init --bare 。 出于兴趣,我从提交git init的--bare选项的提交消息中找到了这个: https://github.com/git/git/commit/74d3b23fe3771c769ccd5c97f6a46682b4061577 该提交包含在git版本v1.5.6及更高版本中。 With such an early version of git, yo ...
-
这是非常伤心的,但umask和 - --shared在Windows Git Bash中没有做任何事情: Developer@BUSHCOMP MINGW64 /d/temp/000 $ ls -l readme.txt -rw-r--r-- 1 Developer Domain users 0 Sep 10 16:44 readme.txt Developer@BUSHCOMP MINGW64 /d/temp/000 $ chmod a+rw readme.txt Developer@BUSHCOMP ...
-
我安装了msysgit,它也失败了,但是有一个更好的错误信息(预期的FS格式'2';找到格式'4'),这引发了对Google的有益讨论。 似乎git-svn无法处理当前TortoiseSVN的本地文件系统格式。 解决方案是运行svnserve并通过localhost使用svn:protocol。 I installed msysgit and it failed as well, but with a much better error message (Expected FS format '2'; fo ...
-
Git:init,在一个命令中添加all并使用message进行提交(Git: init, add all and commit with message in one single command)[2022-01-21]
创建别名: git config --global alias.here '!git init . && git add . && git commit --allow-empty -m "Initialize repository"' 然后像它一样使用它 git here 请注意,我在git commit添加了--allow-empty选项,这将使其在一个空目录和一个包含内容的目录中工作。 Create an alias: git config --global alias.here '!git in ...