在目录中查找最新文件,然后选择具有相同DateTime的所有文件(Find newest file in directory, then select all files with same DateTime)
使用此命令,我可以在目录中搜索最新文件:
gci C:\temp | sort LastWriteTime -descending | select -first 1
我的目录相当大,包含60'000 + txt文件。 运行此命令需要:
PS C:\xy> measure-command {gci C:\temp | sort LastWriteTime -descending | select -first 1} Days : 0 Hours : 0 Minutes : 0 Seconds : 13 Milliseconds : 465 Ticks : 134657703 TotalDays : 0.000155853822916667 TotalHours : 0.00374049175 TotalMinutes : 0.224429505 TotalSeconds : 13.4657703 TotalMilliseconds : 13465.7703
如您所见,此命令需要很长时间才能完成。
我的任务是获取具有与命令返回的文件相同的
LastWriteTime
属性的所有文件(到分钟)。我试过这样的事情:
$file = gci C:\temp -OutVariable files | sort LastWriteTime -descending | select -first 1 | % { $_.LastWriteTime } $myfiles = $files | ? {$_.LastWriteTime -like $file}
当我比较找到LastWriteTime的
$file
和应该包含具有相同LastWriteTime的所有文件的$myfiles
,$myfiles
总是只包含一个文件 - 这是因为每个对象的秒数不同:PS C:\xy> $file Montag, 21. November 2016 13:10:08
如何通过比较lastwritetime属性找到我需要的文件?
With this command, I can search for the newest file in a directory:
gci C:\temp | sort LastWriteTime -descending | select -first 1
My directory is rather large, containing 60'000+ txt files. Running this command takes:
PS C:\xy> measure-command {gci C:\temp | sort LastWriteTime -descending | select -first 1} Days : 0 Hours : 0 Minutes : 0 Seconds : 13 Milliseconds : 465 Ticks : 134657703 TotalDays : 0.000155853822916667 TotalHours : 0.00374049175 TotalMinutes : 0.224429505 TotalSeconds : 13.4657703 TotalMilliseconds : 13465.7703
As you can see, this command takes a long time to finish.
My task is to get all the files which have the same
LastWriteTime
Property as the file which the command returns (to the minute).I tried something like this:
$file = gci C:\temp -OutVariable files | sort LastWriteTime -descending | select -first 1 | % { $_.LastWriteTime } $myfiles = $files | ? {$_.LastWriteTime -like $file}
when I compare
$file
which is the found LastWriteTime, and$myfiles
which should contain all files with the same LastWriteTime,$myfiles
always includes only one file - this is because the seconds are different for each object:PS C:\xy> $file Montag, 21. November 2016 13:10:08
How can I find the files I need by comparing their lastwritetime properties?
原文:https://stackoverflow.com/questions/40720034
最满意答案
如果要使用完全相同的配置实例(类Props),可以将其实例绑定为具有提供程序绑定的单例 。 这当然不是唯一的解决方案,但对我来说这是有道理的。
这是一个例子:
定义提供者:
public class PropsProvider implements Provider<Props> { @Override public Props get() { ...read and return Props here... } }
在单例范围内使用提供程序绑定:
bind(Props.class).toProvider(PropsProvider.class).in(Singleton.class);
注入您的配置:
@Inject public Connection(Props props) { this.props = props; }
您可以阅读文档:
单身人士最适合:
- 有状态对象,例如配置或计数器
- 构造或查找昂贵的对象
- 占用资源的对象,例如数据库连接池。
也许您的配置对象与第一个和第二个条件匹配。 我会避免从模块中读取配置。 在这看到原因。
我在一些单元测试案例中使用按需注入,我想在被测组件中注入模拟依赖项并使用字段注入(这就是为什么我试图避免现场注入:-))我宁愿不使用InjectMocks由于某些原因。
这是一个示例:
零件:
class SomeComponent { @Inject Dependency dep; void doWork() { //use dep here } }
测试本身:
@RunWith(MockitoJUnitRunner.class) public class SomeComponentTest { @Mock private Dependency mockDependency; private SomeComponent componentToTest; @Before public void setUp() throws Exception { componentToTest = new SomeComponent(); Injector injector = Guice.createInjector(new AbstractNamingModule() { @Override protected void configure() { bind(Dependency.class).toInstance(mockDependency); } }); injector.injectMembers(componentToTest); } @Test public void test() { //test the component and/or proper interaction with the dependency } }
If you want to use the very same configuration instance (of class Props) you could bind its instance as a singleton with a provider binding. This is of course not the only solution, but it makes sense for me.
Here is an example:
Define a provider:
public class PropsProvider implements Provider<Props> { @Override public Props get() { ...read and return Props here... } }
Use a provider binding in singleton scope:
bind(Props.class).toProvider(PropsProvider.class).in(Singleton.class);
Inject your configuration:
@Inject public Connection(Props props) { this.props = props; }
You may read in the documentation:
Singletons are most useful for:
- stateful objects, such as configuration or counters
- objects that are expensive to construct or lookup
- objects that tie up resources, such as a database connection pool.
Maybe your configuration object matches the first and the second criteria. I would avoid reading the configuration from within the module. See why here.
I've used on-demand injection in a few unit test cases where I wanted to inject mock dependencies in the component under test and field injection was used (that's why I try to avoid field injections :-) ) AND I preferred not to use InjectMocks for certain reasons.
Here is a sample:
Component:
class SomeComponent { @Inject Dependency dep; void doWork() { //use dep here } }
The test itself:
@RunWith(MockitoJUnitRunner.class) public class SomeComponentTest { @Mock private Dependency mockDependency; private SomeComponent componentToTest; @Before public void setUp() throws Exception { componentToTest = new SomeComponent(); Injector injector = Guice.createInjector(new AbstractNamingModule() { @Override protected void configure() { bind(Dependency.class).toInstance(mockDependency); } }); injector.injectMembers(componentToTest); } @Test public void test() { //test the component and/or proper interaction with the dependency } }
相关问答
更多-
Guice可选注射(Optional injection in Guice)[2022-02-07]
我认为首选的Guice模式是这样的: public class HolderPatter { static class Bar { @Inject Bar(BarDependency dependency) {} } static class Baz { @Inject Baz(BazDependency dependency) {} } static class BarHolder { @Inject(optional=true) Bar value = n ... -
按需要求(clojure on-demand require)[2021-11-11]
是。 它是。 (defn foo [x] (require 'clojure.string) ((resolve 'clojure-string/split) x #"\s")) Yes. It is. (defn foo [x] (require 'clojure.string) ((resolve 'clojure-string/split) x #"\s")) -
您是否尝试在每个构造函数中注入Provider
而不是实际实例? 如果你不需要构造函数代码中的其他实例,那么只需将Provider存储到final字段,稍后再使用该字段(通过调用get() )。 Did you try injecting a Provider in each constructor instead of the actual instances? If you don't need the other instance in the code of constructor, th ... -
引导注射器(bootstrapping guice injector)[2021-11-10]
通常,是:注入Provider以便稍后获取您的对象,并且不会在必要时直接保留您的注入器。 理想情况下,您的引导程序应该就是:它应该实例化您的Injector并获得某种整个应用程序实例 。 这一点尤为重要,因为Guice有助于测试,因此最大化Guice应用程序的一部分是一个好主意。 public class YourApplication { public static void main(String[] args) { // Only three lines exist outside th ... -
Guice部分注射实例(Guice partial injection example)[2022-06-14]
你是对的,这种注射只能通过使用工厂来解决。 如果你有Foo(A a, B b)注入A并且B在运行时传递,你将需要一个Factory FooFactory.createFoo(B b) ,它在内部保存对A的引用。 幸运的是,Guice有@Assisted Injection的概念。 您必须提供工厂的界面,但可以将实现的“魔力”留给Guice。 它有很好的文档记录: https : //github.com/google/guice/wiki/AssistedInject 您需要额外依赖com.google.i ... -
按需javascript(On-Demand javascript)[2022-09-11]
如果你真的想使用AJAX(如问题中所述),你也可以这样做。 首先,您通常使用XMLHttpRequest下载javascript,下载完成后,您可以eval()或将其插入生成的标记中。 function xhr_load(url, callback) { xhr = new XMLHttpRequest(); xhr.onreadystatechange = function() { if (xhr.readyState == 4) { if (xhr.status == 200 ... -
Guice按需注射(Guice on-demand injection)[2023-07-27]
如果要使用完全相同的配置实例(类Props),可以将其实例绑定为具有提供程序绑定的单例 。 这当然不是唯一的解决方案,但对我来说这是有道理的。 这是一个例子: 定义提供者: public class PropsProvider implements Provider{ @Override public Props get() { ...read and return Props here... } } 在单例范围内使用提供程序绑定: bind ... -
WPF ListBox使用VirtualizingStackPanel作为其项目的布局控件。 您可以将VirtualizingStackPanel设置为仅根据需要使用以下XAML加载项:
WPF ListBox's use a VirtualizingStackPanel as the layout control for its i ... -
看看Binder doc: http://google-guice.googlecode.com/git/javadoc/com/google/inject/Binder.html requireExplicitBindings()可能就是你所需要的。 通过使用模块中的语句,禁用自动绑定,仅注入通过bind()或provides类配置的类。 Apparently, the only way to do this is to write a provider method for each prohibite ...
-
将此绑定放入模块时: bind(IManager.class).to(Manager.class); Guice将尝试创建Manager类的新实例。 它查找使用@Inject注释的一个(但只有一个)构造函数或作为非私有的后备零参数构造函数。 这是Guice将使用的构造函数: @Inject public Manager(Person person) { this.person=person; } 现在遵循相同的规则,Guice会尝试使用适当的构造函数来实例化Person ,它会卡在这里: @Inj ...